LeetCode OJ: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.

最近使用缓存的问题,看到key和value感觉可能就要使用map了。用一个list维护一个cache,然后map指向相应cache的每个节点。

注意这里的最近使用,不管是get还是set都属于最近使用的范畴,代码如下所示:

 1 class LRUCache{
 2 struct cacheNode{
 3     int key;
 4     int val;
 5     cacheNode(int k, int v):
 6     key(k), val(v){}
 7 };
 8 public:
 9     LRUCache(int capacity) {
10         size = capacity;
11     }
12
13     int get(int key) {
14         if(cacheMap.find(key) != cacheMap.end()){
15             auto it = cacheMap[key];
16             cacheList.splice(cacheList.begin(), cacheList, it);
17             cacheMap[key] = cacheList.begin();
18             return cacheList.begin()->val;
19         }else
20             return -1;
21     }
22
23     void set(int key, int value) {
24         if(cacheMap.find(key) != cacheMap.end()){//list中存在该元素
25             auto it = cacheMap[key];
26             cacheList.splice(cacheList.begin(), cacheList, it);
27             cacheMap[key] == cacheList.begin();
28             cacheList.begin()->val = value;
29         }else{                                  //list中不存在该元素
30             if(cacheList.size() == this->size){
31                 cacheMap.erase(cacheList.back().key);
32                 cacheList.pop_back();
33             }
34             cacheList.push_front(cacheNode(key, value));
35             cacheMap[key] = cacheList.begin();
36         }
37     }
38 private:
39     int size;
40     list<cacheNode> cacheList;
41     unordered_map<int , list<cacheNode>::iterator> cacheMap;
42 };
时间: 2024-10-05 09:43:32

LeetCode OJ:LRU Cache(最近使用缓存)的相关文章

LeetCode OJ - 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.

由LeetCode的LRU Cache谈到操作系统中LRU算法

1 class LRUCache{ 2 public: 3 LRUCache(int capacity) { 4 size = capacity; 5 } 6 int get(int key) { 7 if(cacheMap.find(key)==cacheMap.end()) 8 return -1; 9 cacheList.splice(cacheList.begin(),cacheList,cacheMap[key]); 10 cacheMap[key] = cacheList.begin

LeetCode 之 LRU Cache Java实现

LeetCode刷了41道题了,流程是按照戴兄的小书,很多不会的是参考Kim姐的代码,自己用Java抠腚的. 前几天做到了LRU Cache: C++的实现方法大同小异,大都用的是一个list加一个hash,hash中存储list节点地址,每次get从hash中寻key,有则将list相应节点放到链表头,没有则返回-1:每次set先判断hash中是否存在,存在则将相应节点移到表头,重置value值,如果不存在,判定长度是否达到预设的capacity,如果达到,删除表尾节点,新节点插入表头. 但是

【LeetCode】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

Java for LeetCode 146 LRU Cache 【HARD】

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

LeetCode:LRU cache

题目: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 r

【LeetCode】LRU Cache 解决报告

插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! 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 ke

LeetCode (26) 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

[Leetcode][JAVA] 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(

[leetcode]146. 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(