当前位置:网站首页>leetcode-146:LRU 缓存
leetcode-146:LRU 缓存
2022-07-25 20:37:00 【菊头蝙蝠】
leetcode-146:LRU 缓存
题目
请你设计并实现一个满足 LRU (最近最少使用) 缓存 约束的数据结构。
实现 LRUCache 类:
- LRUCache(int capacity) 以 正整数 作为容量 capacity 初始化 LRU 缓存
- int get(int key) 如果关键字 key 存在于缓存中,则返回关键字的值,否则返回 -1 。
- void put(int key, int value) 如果关键字 key 已经存在,则变更其数据值 value ;如果不存在,则向缓存中插入该组 key-value 。如果插入操作导致关键字数量超过 capacity ,则应该 逐出 最久未使用的关键字。
函数 get 和 put 必须以 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
解题
方法一:双向链表+哈希
struct Node{
int key,value;
Node* left;
Node* right;
Node(int _key,int _value):key(_key),value(_value),left(nullptr),right(nullptr){
}
};
class LRUCache {
public:
Node* L;
Node* R;
int n;
unordered_map<int,Node*> hash;
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;
Node* p=hash[key];
remove(p);
insert(p);
return p->value;
}
void put(int key, int value) {
if(hash.count(key)){
//如果key存在,则修改对应的value
Node* p=hash[key];
p->value=value;
remove(p);
insert(p);
}else{
if(hash.size()==n){
//如果缓存已满,则删除双链表最右侧的节点
Node* p=R->left;
remove(p);
hash.erase(p->key);//更新哈希表
delete p;
}
//插入新的节点
Node* p=new Node(key,value);
hash[key]=p;
insert(p);
}
}
void remove(Node* p){
p->right->left=p->left;
p->left->right=p->right;
}
void insert(Node* p){
p->right=L->right;
L->right->left=p;
L->right=p;
p->left=L;
}
};
边栏推荐
猜你喜欢

Detailed explanation of document operation

Mobile web layout method

Difference Between Accuracy and Precision

4everland storage node portal network design

How to choose a microservice registration center?
![[noi simulation] string matching (suffix automata Sam, Mo team, block)](/img/db/3ccb00e78bba293acdae91ffa72a2c.png)
[noi simulation] string matching (suffix automata Sam, Mo team, block)
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation
![[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born](/img/0b/73f0d98a6db813e54074abe199ed98.png)
[today in history] July 4: the first e-book came out; The inventor of magnetic stripe card was born; Palm computer pioneer was born
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

Recommended books | essentials of industrial digital transformation: methods and Practice
随机推荐
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
[today in history] July 18: Intel was founded; The first photo was posted on the world wide web; EBay spins off PayPal
【高等数学】【3】微分中值定理与导数的应用
“链”接无限可能:数字资产链,精彩马上来!
火山引擎项亮:机器学习与智能推荐平台多云部署解决方案正式发布
[advanced drawing of single cell] 07. Display of KEGG enrichment results
DIY个人服务器(diy存储服务器)
Detailed explanation of document operation
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
RF、GBDT、XGboost特征选择方法「建议收藏」
[today in history] July 2: BitTorrent came out; The commercial system linspire was acquired; Sony deploys Playstation now
Cloud native guide: what is cloud native infrastructure
Open source SPL enhances mangodb computing
CarSim simulation quick start (XV) - ADAS sensor objects of CarSim sensor simulation (1)
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
什么是聚类分析?聚类分析方法的类别[通俗易懂]
Card link
【单细胞高级绘图】07.KEGG富集结果展示
Today's sleep quality record 75 points
[workplace rules] it workplace rules | poor performance