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.
题目解析:
先举个栗子
比如我cache只有4这么大,现在有很多元素1,2,2,4,2,5,3
cache income:1
1
cache income:2
2 1
cache income:1
1 2
cache income:4
4 1 2
cache income:2
2 4 1
cache income:5
5 2 4 1
cache income:3
3 5 2 4
大概就这么个样子。。。
最后使用的放最前面
如果cache不满,新来的放第一个,如果满了,在cache里面就把里面那个放到第一个,如果不在就删除最后一个,然后把新元素放第一个。
实现的时候,可以用数组,链表实现,但是由于还有val值,所有就用链表的形式了。可以用单向链表,也可以用双向链表。那怎么查找呢?可以用遍历的方法,从前向后找。但是时间复杂度太高了,就用hash来找。那么就想到了map和unordered_map。
方案一:
map+双向链表
使用双向链表的目的是更容易删除最后的结点。不用维持一个pre指针,或者从前向后遍历到倒数第二个结点。
链表头部的表示刚刚访问过的,链表尾部的表示很久之前访问的
每次get(key),先在map中找到这个节点,然后把这个节点放到链表头部。
每次set(key, value),现在map中找这个节点,如果有的话就把这个节点放到链表头部,如果没有就看看cache空间是否已经满了,size>=capacity,如果未满,就生成一个新的节点放到链表头部,如果满了,就生成一个新的节点放到链表头部并且删除链表尾部的一个节点。
class LRUCache {
map<int, node*> mp;
node* head;
node* tail;
int size;
int capacity;
public:
LRUCache(int c) {
if (c < 1)return;
head = new node(0, 0); //为了刚方便表达,就设置头结点和尾结点,更容易查找。
tail = new node(0, 0);
head->next = tail;
tail->pre = head;
mp.clear();
size = 0;
capacity = c;
}
int get(int k) {
map<int, node*>::iterator it = mp.find(k);
if (it != mp.end()) { //找到以后,提取出来,放到链表头
node* cur = (*it).second;
cur->pre->next = cur->next;
cur->next->pre = cur->pre;
putToHead(cur);
return cur->value;
} else
return -1;
}
void set(int k, int val) {
if (capacity < 1)return;
map<int, node*>::iterator it = mp.find(k);
if (it != mp.end()) {//find
node* cur = (*it).second;
cur->pre->next = cur->next;
cur->next->pre = cur->pre;
cur->value = val;
putToHead(cur);
} else {//not find
node* tmp = new node(k,val);
putToHead(tmp);
mp[k] = tmp; //没找到是,要更新map表
if (size < capacity) {//size < capacity
size++;
} else {//size >= capacity
node* deltmp = tail->pre; //超过容量,删除最后的结点
tail->pre = deltmp->pre;
deltmp->pre->next = tail;
it = mp.find(deltmp->key);
mp.erase(it); //删除时,要接的更新map
delete deltmp;
}
}
}
void putToHead(node* cur)
{
cur->next = head->next;
cur->pre = head;
cur->next->pre = cur;
head->next = cur;
}
};
方案二:Hash+双向链表O(1)
也就是利用unordered_map。修改一下就行。
struct node{
node* pre;
int key;
int value;
node* next;
node(int k, int v):key(k),value(v),pre(NULL),next(NULL){};
};
class LRUCache{
unordered_map<int, node*> mp;
int capacity;
int size;
node* head;
node* tail;
public:
LRUCache(int c){
if(c<0)return;
head = new node(-1,-1);
tail = new node(-1,-1);
head->next = tail;
tail->pre = head;
mp.clear();
capacity = c;
size = 0;
}
int get(int k) {
unordered_map<int, node*>::iterator it = mp.find(k);
if(it != mp.end()){
node* p = it->second;
p->pre->next = p->next;
p->next->pre = p->pre;
putToHead(p);
return p->value;
}
else
return -1;
}
void set(int k, int val) {
if(capacity < 1) return;
unordered_map<int, node*>::iterator it = mp.find(k);
if(it != mp.end()){
node* p = it->second;
p->pre->next = p->next;
p->next->pre = p->pre;
putToHead(p);
p->value = val;
}else{
node* p = new node(k, val);
putToHead(p);
mp[k] = p;
size++;
if(size>capacity){
p = tail->pre;
tail->pre = p->pre;
p->pre->next = tail;
it = mp.find(p->key);
mp.erase(it);
delete p;
}
}
}
void putToHead(node* p)
{
p->next = head->next;
p->pre = head;
head->next->pre = p;
head->next = p;
}
};