zoukankan      html  css  js  c++  java
  • 146. LRU Cache

    问题描述:

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.

    get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
    put(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

    Follow up:
    Could you do both operations in O(1) time complexity?

    Example:

    LRUCache cache = new LRUCache( 2 /* capacity */ );
    
    cache.put(1, 1);
    cache.put(2, 2);
    cache.get(1);       // returns 1
    cache.put(3, 3);    // evicts key 2
    cache.get(2);       // returns -1 (not found)
    cache.put(4, 4);    // evicts key 1
    cache.get(1);       // returns -1 (not found)
    cache.get(3);       // returns 3
    cache.get(4);       // returns 4

    解题思路:

    这道题我是真的没有思路,特别是关于怎样把用过的挪到前面来。

    看了大佬的解法

    之前没有想到要用list这个容器

    用map来存储key和指向list中节点位置的遍历器。

    用cap来存储初始化时,cache的容量

    用list来存储具体的value并且根据操作来挪动节点的位置或者删除节点。

    对于get:

      首先我们在map中找到key对应的list中的遍历器的位置。若为map.end(),则map中不存在这个键值对。

      若存在,我们调用list中的方法splice将其插入到链表头部,然后返回值

    需要注意的是:

      map中存的是list的遍历器!调用find 时,返回的是map的遍历器!所以map->second指向的list的遍历器,map->second->second指向的是list中的值。

    对于put:

      首先我们检查map中是否已经存在key值,若存在,则将原先的键值对删除后进行插入操作若不存在则可直接进行插入操作。

      我们讲新的pair直接插入到链表头部

      此时检查m的大小是否超出容量:

         1.没有超出,则可结束程序

         2.超出,用list方法rbegin()找到最后一个节点并且获取键值key,链表中删除这个节点(用pop_back()),然后在map中根据key删除对应键值对

    代码:

    class LRUCache {
    public:
        LRUCache(int capacity) {
            cap = capacity;
        }
        
        int get(int key) {
            auto it = m.find(key);
            if(it == m.end())
                return -1;
            l.splice(l.begin(), l, it->second);
            return it->second->second;
        }
        
        void put(int key, int value) {
            auto it = m.find(key);
            if(it != m.end())
                l.erase(it->second);
            l.push_front(pair<int,int>(key, value));
            m[key] = l.begin();
            if(m.size() > cap){
                int k = l.rbegin()->first;
                l.pop_back();
                m.erase(k);
            }
        }
    private:
        int cap;
        list<pair<int, int>> l;
        unordered_map<int, list<pair<int,int>>::iterator> m;
    };
    
    /**
     * Your LRUCache object will be instantiated and called as such:
     * LRUCache obj = new LRUCache(capacity);
     * int param_1 = obj.get(key);
     * obj.put(key,value);
     */
  • 相关阅读:
    linux 权限管理命令
    大三上学期总结
    C# 读写Excel的一些方法,Aspose.Cells.dll
    Topshelf 创建.net服务整理和安装步骤(转)
    你必须知道的.NET之特性和属性(转)
    用SQL语句删除一个数据库的所有表和所有存储过程
    System.DllNotFoundException: 无法加载 DLL“FileTracker.dll”: 动态链接库(DLL)初始化例
    关于消息队列的使用[转]
    PhpStorm使用技巧小结
    转载]C#实现获取浏览器信息
  • 原文地址:https://www.cnblogs.com/yaoyudadudu/p/9176472.html
Copyright © 2011-2022 走看看