LeetCode - LRU cache
https://leetcode-cn.com/problems/lru-cache/
1. 支持查找,缓存不存在返回-1;存在的话,返回缓存的值,这里使用map存储缓存节点的映射关系,并且将缓存节点放置到最前端。
2. 支持快速删除,用双向链表来做删除。
#include <iostream> #include <map> using namespace std; class CacheNode { public: CacheNode* pre; CacheNode* next; int k; int v; CacheNode(int k, int v):k(k),v(v) { pre = NULL; next = NULL; }; }; class LRUCache { private: int cache_capacity; CacheNode* head; CacheNode* tail; int size; map<int, CacheNode*> cache_map; public: LRUCache(int capacity) { head = NULL; tail = NULL; cache_capacity = capacity; size = 0; } int getNode(int key) { //get cache node ptr int ret = 0; if(cache_map.find(key) == cache_map.end()) { return -1; } ret = cache_map[key]->v; removeNode(key); addNode(key,ret); return ret; } int get(int key) { // cout<<getNode(key)<<endl; // Print(); return getNode(key); } void removeNode(int key) { if(cache_map.find(key) == cache_map.end() || size <= 0) return; size--; map<int, CacheNode*>::iterator iter = cache_map.find(key); CacheNode* cn = iter->second; cache_map.erase(iter); if(cn->next){ cn->next->pre = cn->pre; } if(cn->pre) { cn->pre->next = cn->next; } if(cn == head) { head = head->next; } if(cn == tail) { tail = tail->pre; } delete cn; return; } void addNode(int key, int value) { size++; CacheNode* p = new CacheNode(key,value); cache_map[key] = p; if(size == 1) { head = p; tail = p; tail->next = NULL; head->pre = NULL; } else { p->next = head; head->pre = p; head = p; } return; } void put(int key, int value) { //if exist if(cache_map.find(key) != cache_map.end()) { removeNode(key); addNode(key,value); } else { if(size < cache_capacity) { addNode(key,value); } else { //del tail removeNode(tail->k); addNode(key,value); } } } }; int main() { LRUCache* cache_ptr = new LRUCache(2); LRUCache cache = *cache_ptr; cache.put(2, 1); cache.put(3,2); cache.get(3); // 返回 2 cache.get(2); // 返回 1 cache.put(4, 3); // 该操作会使得关键字 3 作废 cache.get(2); // 返回 1 cache.get(3); // 返回 -1 cache.get(4); // 返回 3 return 0; }
#include <iostream>
#include <map>
using namespace std;
class CacheNode
{
public:
CacheNode* pre;
CacheNode* next;
int k;
int v;
CacheNode(int k, int v):k(k),v(v)
{
pre = NULL;
next = NULL;
};
};
class LRUCache {
private:
int cache_capacity;
CacheNode* head;
CacheNode* tail;
int size;
map<int, CacheNode*> cache_map;
public:
LRUCache(int capacity)
{
head = NULL;
tail = NULL;
cache_capacity = capacity;
size = 0;
}
int getNode(int key) {
//get cache node ptr
int ret = 0;
if(cache_map.find(key) == cache_map.end())
{
return -1;
}
ret = cache_map[key]->v;
removeNode(key);
addNode(key,ret);
return ret;
}
int get(int key) {
// cout<<getNode(key)<<endl;
// Print();
return getNode(key);
}
void removeNode(int key)
{
if(cache_map.find(key) == cache_map.end() || size <= 0)
return;
size--;
map<int, CacheNode*>::iterator iter = cache_map.find(key);
CacheNode* cn = iter->second;
cache_map.erase(iter);
if(cn->next){
cn->next->pre = cn->pre;
}
if(cn->pre)
{
cn->pre->next = cn->next;
}
if(cn == head)
{
head = head->next;
}
if(cn == tail)
{
tail = tail->pre;
}
delete cn;
return;
}
void addNode(int key, int value)
{
size++;
CacheNode* p = new CacheNode(key,value);
cache_map[key] = p;
if(size == 1)
{
head = p;
tail = p;
tail->next = NULL;
head->pre = NULL;
}
else
{
p->next = head;
head->pre = p;
head = p;
}
return;
}
void put(int key, int value)
{
//if exist
if(cache_map.find(key) != cache_map.end())
{
removeNode(key);
addNode(key,value);
}
else
{
if(size < cache_capacity)
{
addNode(key,value);
}
else
{
//del tail
removeNode(tail->k);
addNode(key,value);
}
}
}
};
int main()
{
LRUCache* cache_ptr = new LRUCache(2);
LRUCache cache = *cache_ptr;
cache.put(2, 1);
cache.put(3,2);
cache.get(3); // 返回 2
cache.get(2); // 返回 1
cache.put(4, 3); // 该操作会使得关键字 3 作废
cache.get(2); // 返回 1
cache.get(3); // 返回 -1
cache.get(4); // 返回 3
return 0;
}