[LeetCode] LRU Cache

简介: The basic idea is to store the key-value pair in some container. In the following code, we make three type definitions: 1 typedef list LI; 2 typed...

The basic idea is to store the key-value pair in some container. In the following code, we make three type definitions:

1 typedef list<int> LI;
2 typedef pair<int, LI::iterator> PII;
3 typedef unordered_map<int, PII> HIPII;

HIPII is the ultimate type for the container. It is essentially an unordered_map, using key as its key and a pair as its value. The first element of the pair is the value and the second element of it is the iterator of the key in a list (front elements are more recently used).  So each element in the HIPII container is like a {int key, {int value, iterator}}.

Now the implementation of get and set is relatively straightforward: each time we access a key, touch it (making it at the beginning of the list) and then get or set its value. If the container has met its size and we need add a new key, just remove the key corresponding to the last element of list (least recently used).

The code is as follows.

 1 class LRUCache{
 2 public:
 3     LRUCache(int capacity) {
 4         _capacity = capacity;
 5     }
 6     
 7     int get(int key) {
 8         auto it = cache.find(key);
 9         if (it == cache.end()) return -1;
10         touch(it);
11         return it -> second.first;
12     }
13     
14     void set(int key, int value) {
15         auto it = cache.find(key);
16         if (it != cache.end()) touch(it);
17         else {
18             if (cache.size() == _capacity) {
19                 cache.erase(used.back());
20                 used.pop_back();
21             }
22             used.push_front(key);
23         }
24         cache[key] = {value, used.begin()};
25     }
26 private:
27     typedef list<int> LI;
28     typedef pair<int, LI::iterator> LII;
29     typedef unordered_map<int, LII> HIPII;
30     
31     int _capacity;
32     LI used;
33     HIPII cache;
34     
35     void touch(HIPII::iterator it) {
36         int key = it -> first;
37         used.erase(it -> second.second);
38         used.push_front(key);
39         it -> second.second = used.begin();
40     }
41 };

 

目录
相关文章
|
5月前
|
存储 缓存 算法
☆打卡算法☆LeetCode 146. LRU 缓存 算法解析
☆打卡算法☆LeetCode 146. LRU 缓存 算法解析
|
12月前
|
存储 缓存
图解LeetCode——146. LRU 缓存
图解LeetCode——146. LRU 缓存
88 1
|
缓存
leetcode 146 LRU缓存
leetcode 146 LRU缓存
86 0
leetcode 146 LRU缓存
|
缓存 算法
LeetCode 146. LRU Cache
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制。它应该支持以下操作: 获取数据 get 和 写入数据 put 。
72 0
LeetCode 146. LRU Cache
|
缓存 Java C++
【LeetCode146】LRU缓存机制(list+unordered_map)
首先读懂题意: 首先缓存最大容量初始化为capacity这么大,然后实现: put(key,val)存入键值对;get(key)获得键key对应的值val,如果键key不存在则返回-1。
223 0
|
缓存 算法 NoSQL
LeetCode解题之十八:LRU
LeetCode解题之十八:LRU
|
存储 缓存 API
LeetCode——LRU 缓存机制(借助Map)
LeetCode——LRU 缓存机制(借助Map)
77 0
LeetCode——LRU 缓存机制(借助Map)
|
存储 缓存 API
LeetCode——LRU 缓存机制(借助Map)
解决这个问题之前,我们首先要读懂题意,知道什么是LRU缓存机制,LRU缓存机制指的是优先删除那些最久未使用的缓存,在本题中,一个缓存被put或者get都算是一次使用,明白这一点,也就理解了本题的核心题意。
134 0
LeetCode——LRU 缓存机制(借助Map)

热门文章

最新文章