LeetCode – Refresh – LRU

 1 struct Node {
 2     int key, value;
 3     Node(int k, int v) : key(k), value(v) {}
 4 };
 5
 6 class LRUCache{
 7 private:
 8     int size;
 9     list<Node> nlist;
10     unordered_map<int, list<Node>::iterator> nmap;
11 public:
12     LRUCache(int capacity) {
13         size = capacity;
14     }
15
16     int get(int key) {
17         if (nmap.find(key) != nmap.end()) {
18             nlist.splice(nlist.begin(), nlist, nmap[key]);
19             nmap[key] = nlist.begin();
20             return nlist.begin()->value;
21         }
22         return -1;
23     }
24
25     void set(int key, int value) {
26         if (nmap.find(key) == nmap.end()) {
27             if (size == nlist.size()) {
28                 nmap.erase(nlist.back().key);
29                 nlist.pop_back();
30             }
31             nlist.push_front(Node(key, value));
32             nmap[key] = nlist.begin();
33         } else {
34             nlist.splice(nlist.begin(), nlist, nmap[key]);
35             nmap[key] = nlist.begin();
36             nlist.begin()->value = value;
37         }
38     }
39 };
时间: 2024-10-11 06:33:24

LeetCode – Refresh – LRU的相关文章

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

LeetCode - Refresh - Pascal&#39;s Triangle II

Exact same as I. 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 if (rowIndex < 0) return vector<int> (); 5 vector<int> result(1, 1); 6 for (int i = 1; i <= rowIndex; i++) { 7 for (int j = result.size()-1; j >

LeetCode - Refresh - Pascal&#39;s Triangle

This is simple. Just everything use current[j] += current[j-1]. But leave the first one to be "1". Then add another "1" to end. 1 class Solution { 2 public: 3 vector<vector<int> > generate(int numRows) { 4 vector<vector&

由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 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.

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实现

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