题目
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。
实现 LRUCache 类:
LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作?
解析
使用双链表+哈希表存储
代码
class LRUCache {
public:
struct Node{
int key, val;
Node *left;
Node *right;
Node(int _key, int _val):key(_key), val(_val), left(nullptr), right(nullptr){
}
}*L, *R;
unordered_map<int, Node*> hash; // 哈希表
int n;
void remove(Node *p){
p->right->left =p->left;
p->left->right = p->right;
}
void insert(Node *p){
p->right = L->right;
p->left = L;
L->right->left = p;
L->right = p;
}
LRUCache(int capacity) {
n = capacity;
L = new Node(-1, -1), R = new Node(-1, -1);
L->right = R;
R->left = L;
}
int get(int key) {
if(hash.count(key)==0) return -1;
auto p = hash[key];
remove(p);
// 将该点插入到最左侧
insert(p);
return p->val;
}
void put(int key, int value) {
if(hash.count(key)){
auto p = hash[key];
p->val = value;
remove(p);
insert(p);
}else{
if(hash.size()==n){
auto p = R->left;
remove(p);
hash.erase(p->key);
delete p; // 考虑可能存在内存溢出的情况
}
auto p= new Node(key, value);
hash[key]=p;
insert(p);
}
}
};
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/