题目描述:
运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制 。
实现 LRUCache 类:
LRUCache(int capacity) 以正整数作为容量 capacity 初始化 LRU 缓存
int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
void put(int key, int value) 如果关键字已经存在,则变更其数据值;如果关键字不存在,则插入该组「关键字-值」。当缓存容量达到上限时,它应该在写入新数据之前删除最久未使用的数据值,从而为新的数据值留出空间。
进阶:你是否可以在 O(1) 时间复杂度内完成这两种操作?
示例:
输入
[“LRUCache”, “put”, “put”, “get”, “put”, “get”, “put”, “get”, “get”, “get”]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
输出
[null, null, null, 1, null, -1, null, -1, 3, 4]
解释
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // 缓存是 {1=1}
lRUCache.put(2, 2); // 缓存是 {1=1, 2=2}
lRUCache.get(1); // 返回 1
lRUCache.put(3, 3); // 该操作会使得关键字 2 作废,缓存是 {1=1, 3=3}
lRUCache.get(2); // 返回 -1 (未找到)
lRUCache.put(4, 4); // 该操作会使得关键字 1 作废,缓存是 {4=4, 3=3}
lRUCache.get(1); // 返回 -1 (未找到)
lRUCache.get(3); // 返回 3
lRUCache.get(4); // 返回 4
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lru-cache
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/lru-cache
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路:后续更新
代码:
struct DLinkNode{//双向链表结点
DLinkNode* prex;
DLinkNode* next;
int key;
int value;
DLinkNode(){//构造函数
this->key = 0;
this->value = 0;
this->prex = NULL;
this->next = NULL;
}
DLinkNode(int key,int value){//构造函数
this->key = key;
this->value = value;
this->prex = NULL;
this->next = NULL;
}
};
class LRUCache {
private:
int capacity;
int size;
unordered_map< int , DLinkNode* > cache;
DLinkNode* head;
DLinkNode* tail;
public:
//链表最前面表示的是最新的,最后面表示老的要删除的
LRUCache(int capacity) {//构造函数,用于实例化
this->capacity = capacity;
this->size = 0;
this->head = new DLinkNode();
this->tail = new DLinkNode();
this->head->next = this->tail;
this->tail->prex = this->head;
}
int get(int key) {
//根据key查找
auto it = cache.find(key);
if(it == cache.end()){//没找到key,find方法返回迭代器end()
return -1;
}
//将当前这个key指向的结点移到最前面
DLinkNode* node = it->second;
moveToHead(node);
return node->value;
}
void put(int key, int value) {
//先判断关键字是否存在
auto it = cache.find(key);
if(it == cache.end()){//不存在
//判断cache容量是否满,满则删除最老的数据,否则直接插入
if(this->size == this->capacity){
//删除最后一个结点
removeNode();
this->size--;
}
//增加一个结点
DLinkNode* newNode = new DLinkNode(key,value);
addNodeToHead(newNode);//添加至链表
cache[key] = newNode;//添加至哈希表
this->size++;
}
else{//存在
DLinkNode* node = it->second;
node->value = value;
moveToHead(node);
}
/*也可以这样写,unordered_map的[]如果找不到就执行插入
cache[key] = value;
*/
}
//将对链表的各种操作封装起来
void moveToHead(DLinkNode* node){
//移动即删除这个结点再插入到头部
node->prex->next = node->next;
node->next->prex = node->prex;
addNodeToHead(node);
}
void removeNode(){
int reKey = tail->prex->key;
DLinkNode* reNode = tail->prex;
//更改指针并释放结点,防止内存泄漏
reNode->prex->next = reNode->next;
tail->prex = reNode->prex;
delete reNode;
//删除容器里的元素
cache.erase(reKey);
}
void addNodeToHead(DLinkNode* newNode){
newNode->next = head->next;
newNode->prex = head;
head->next->prex = newNode;
head->next = newNode;
}
};
/**
* 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);
*/