题目来源
题目描述
class LRUCache {
public:
LRUCache(int capacity) {
}
int get(int key) {
}
void put(int key, int value) {
}
};
题目解析
如果容量不够,那么要求删除某些key
删除衡量点:上次使用时间里现在越远,那么就先删除
双向链表 + 哈希表
- 用双向链表来表说使用频率:
- 离链表头部越近说明上次使用时间越远
- 里链表头部越远说明上次使用时间越近
- 用哈希表来快速定位节点在哪里
有三个参数:
(1)构造函数
- 参数为capacity
- 这表示哈希表最多能存储capacity个元素,如果多了就要淘汰
(2)put
- 参数为 put(int key, int value)
- 当调用put时,有两种可能
- 当前key已经存在,说明是修改操作
- 将对应节点移动到链表尾部
- 当前key是新的
- 根据(key,value)来new一个节点
- 将这个节点插入链表尾部
- 检测cache节点数是否为capacity+1,如果是,那么将链表头部的节点从双向链表和哈希表中擦除
- 当前key已经存在,说明是修改操作
(3)get
- get(int key)
- 当调用get时,有两种可能
- 当前key不存在,返回-1表示未找到
- 当前key存在,那么通过cache找出对应节点的位置,然后将这个节点移动到链表尾部
双向链表对应操作小结:
- 将对应节点移动到链表尾部
- 将对应节点插入链表尾部
- 将链表头部节点移除
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <map>
#include <list>
using namespace std;
class Node{
public:
int key;
int value;
Node* last;
Node* next;
Node(int key, int value){
this->key = key;
this->value = value;
}
};
class NodeDoubleLinkedList{
private:
Node *head;
Node *tail;
public:
NodeDoubleLinkedList() : head(nullptr), tail(nullptr){
}
// 现在来了一个新的node,请挂到尾巴上去
void addNode(Node *newNode){
if(newNode == nullptr){
return;
}
if(head == nullptr){
head = newNode;
tail = newNode;
}else{
tail->next = newNode;
newNode->last = tail;
tail = newNode;
}
}
// 一定会保证node 入参在双向链表里!
// node原始的位置,左右重新连好,然后把node分离出来
// 挂到整个链表的尾巴上
void moveNodeToTail(Node *node){
if(tail == node){
return;
}
if(head == node){
head = node->next;
head->last = nullptr;
}else{
node->last->next = node->next;
node->next->last = node->last;
}
node->last = tail;
node->next = nullptr;
tail->next = node;
tail = node;
}
Node *removeHead(){
if(head == nullptr){
return nullptr;
}
auto res = head;
if (head == tail) {
head = nullptr;
tail = nullptr;
} else {
head = res->next;
res->next = nullptr;
head->last = nullptr;
}
return res;
}
};
class LRUCache {
private:
std::map<int, Node*> keyNodeMap;
NodeDoubleLinkedList *nodeList;
int capacity;
private:
void removeMostUnusedCache(){
auto removeNode = nodeList->removeHead();
keyNodeMap.erase(removeNode->key);
}
public:
LRUCache(int capacity) : capacity(capacity){
}
int get(int key) {
if(keyNodeMap.count(key)){
auto res = keyNodeMap[key];
nodeList->moveNodeToTail(res);
return res->value;
}
return -1;
}
void put(int key, int value) {
if(keyNodeMap.count(key)){
auto node = keyNodeMap[key];
node->value = value;
nodeList->moveNodeToTail(node);
}else{
auto newNode = new Node(key, value);
keyNodeMap[key] = newNode;
nodeList->addNode(newNode);
if(keyNodeMap.size() == capacity + 1){
removeMostUnusedCache();
}
}
}
};
type entry struct {
key, value int
}
type LRUCache struct {
capacity int
list *list.List // 双向链表
keyToNode map[int]*list.Element
}
func Constructor(capacity int) LRUCache {
return LRUCache{capacity, list.New(), map[int]*list.Element{}}
}
func (c *LRUCache) Get(key int) int {
node := c.keyToNode[key]
if node == nil { // 没有这本书
return -1
}
c.list.MoveToFront(node) // 把这本书放在最上面
return node.Value.(entry).value
}
func (c *LRUCache) Put(key, value int) {
if node := c.keyToNode[key]; node != nil { // 有这本书
node.Value = entry{key, value} // 更新
c.list.MoveToFront(node) // 把这本书放在最上面
return
}
c.keyToNode[key] = c.list.PushFront(entry{key, value}) // 新书,放在最上面
if len(c.keyToNode) > c.capacity { // 书太多了
delete(c.keyToNode, c.list.Remove(c.list.Back()).(entry).key) // 去掉最后一本书
}
}
类似题目
题目 | 思路 |
---|---|
leetcode:146. LRU最近最少使用 缓存 LRU Cache | 一个哈希表 + 一个双向链表 |
leetcode:460. LFU最不常用缓存 LFU Cache | 两个哈希表 + N个双向链表 |
588. 设计内存文件系统Design In-Memory File System |