LeetCode题解:LRU Cache

原创 2013年12月02日 16:24:01

LRU Cache

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

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(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.

思路:

维护两个结构。一个红黑树结构用于O(log N)搜索,另一个链表用于排列最新访问的数据。

几个值得注意的地方。std::list::remove会遍历每个元素,并删除符合条件的元素。而在程序中元素的值是唯一的,所以应该调用std::find,然后做std::list::erase。另外用了unordered_map,这样平均访问时间是O(1),用来通过一些消耗时间较长的测试用例。但是反过来占用了大量内存。

题解:

class LRUCache
{
    int m_Capacity;
    unordered_map<int, int> m_Cache;
    list<int> m_Recently;

    void touch(int key)
    {
        auto kiter = find(begin(m_Recently), end(m_Recently), key);
        m_Recently.erase(kiter);
        m_Recently.push_front (key);
    }
public:
    LRUCache (int capacity) : m_Capacity (capacity)
    {}
    
    int get (int key)
    {
        auto iter = m_Cache.find (key);
        if (iter == end(m_Cache))
            return -1;  // not found
        else
        {
            touch(key);
            return iter->second;
        }
    }
    
    void set (int key, int value)
    {                                                                                                  
        auto iter = m_Cache.find (key);                                                                
                                                                                                       
        if (iter == end(m_Cache))
        {                        
            if  (m_Cache.size() >= m_Capacity)
            {
                int last = m_Recently.back();                                                              
                m_Recently.pop_back();                                                                     
                m_Cache.erase (last);
            }
            m_Recently.push_front(key);
        }
                                                                                                       
        m_Cache[key] = value;
        touch(key);
    }
};



LRU Cache算法实现(leetcode)

题意实现一个LRU cache类,使得在我们需要获取一个key的值时,如果cache中有,那么直接返回值,否则就返回-1;同时我们还可以设置一个cache的值,这个主要是在我们出现cache miss...

[LeetCode]LRU Cache有个问题,求大神解答【已解决】

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support th...

LeetCode:LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...

LRU Cache [leetcode]

使用双向链表+map,实现O(1)时间内的get和set 需要注意的是: 1. set时更新tail size为0时更新头部 size为capacity时删除头部并且更新头部 2. get时...

[Leetcode]146. LRU Cache @python

题目Design and implement a data structure for Least Recently Used (LRU) cache. It should support the f...

LRU Cache (leetcode)

题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should suppor...

LeetCode (26) LRU Cache

题目描述Design and implement a data structure for Least Recently Used (LRU) cache. It should support the...

#leetcode#LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...

Leetcode 146 - LRU Cache(HashMap + List)

题意实现LRU缓存(Least Recently Used)。 要求:所有操作都是O(1)O(1)的。 LRU缓存机制:假设我们能够缓存的数目为n。在加入新元素时:当容量小于n时,我们直接加入新元...
  • Lzedo
  • Lzedo
  • 2017年03月12日 19:39
  • 129

leetcode146~LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:LeetCode题解:LRU Cache
举报原因:
原因补充:

(最多只允许输入30个字)