s0146
This commit is contained in:
65
src/s0146_lru_cache.cpp
Normal file
65
src/s0146_lru_cache.cpp
Normal file
@@ -0,0 +1,65 @@
|
||||
#include "s0146_lru_cache.hpp"
|
||||
|
||||
void LRUCache::moveToHead(CacheNode *node) {
|
||||
// 如果是头部节点
|
||||
if (node == head) return;
|
||||
// 如果不是头部节点,但是是尾部节点
|
||||
if (node == tail) tail = node->prev;
|
||||
// 处理该节点的前后两个节点的指针
|
||||
if (node->prev) node->prev->next = node->next;
|
||||
if (node->next) node->next->prev = node->prev;
|
||||
// 将该节点移动到头部
|
||||
node->prev = nullptr;
|
||||
node->next = head;
|
||||
// 处理头部节点
|
||||
head->prev = node;
|
||||
head = node;
|
||||
}
|
||||
|
||||
LRUCache::LRUCache(int capacity) {
|
||||
this->capacity = capacity;
|
||||
head = nullptr;
|
||||
tail = nullptr;
|
||||
map = std::unordered_map<int, CacheNode *>();
|
||||
}
|
||||
|
||||
int LRUCache::get(int key) {
|
||||
if (map.size() == 0) return -1;
|
||||
if (map.count(key) == 0) return -1;
|
||||
moveToHead(map[key]);
|
||||
return map[key]->value;
|
||||
}
|
||||
|
||||
void LRUCache::put(int key, int value) {
|
||||
// 如果 key 已存在,则更新 value ,并将这个节点移动到头部
|
||||
if (map.count(key) == 1) {
|
||||
map[key]->value = value;
|
||||
moveToHead(map[key]);
|
||||
return;
|
||||
}
|
||||
// 否则创建该节点
|
||||
CacheNode *newNode = (CacheNode *)malloc(sizeof(CacheNode));
|
||||
newNode->value = value;
|
||||
newNode->key = key;
|
||||
newNode->next = head;
|
||||
newNode->prev = nullptr;
|
||||
// 处理头部节点
|
||||
if (head) head->prev = newNode;
|
||||
head = newNode;
|
||||
// 处理尾部节点
|
||||
if (map.size() == 0) tail = newNode;
|
||||
// 更新哈希表
|
||||
map[key] = newNode;
|
||||
// 如果容量已满
|
||||
if (map.size() > capacity) {
|
||||
// 更新尾部节点
|
||||
CacheNode *node = tail;
|
||||
if (tail->prev) {
|
||||
tail->prev->next = nullptr;
|
||||
tail = tail->prev;
|
||||
}
|
||||
// 移除该节点
|
||||
map.erase(node->key);
|
||||
free(node);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user