当前位置:网站首页>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;
}
};
边栏推荐
- CarSim simulation quick start (XIV) - CarSim Simulink joint simulation
- [advanced mathematics] [5] definite integral and its application
- Unity VS—— VS中默认调试为启动而不是附加到Unity调试
- Why did I choose to become a network engineer after graduating from weak current for 3 months
- [tensorrt] dynamic batch reasoning
- Rand1 generates rand9
- CarSim simulation quick start (16) - ADAS sensor objects of CarSim sensor simulation (2)
- Key network protocols in tcp/ip four layer model
- 智能电子界桩自然保护区远程监控解决方案
- [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] [6] differential calculus of multivariate functions](/img/9e/84fe6f74b58cbaabab1b6eed0df556.png)
[advanced mathematics] [6] differential calculus of multivariate functions
![[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger](/img/14/f2b68dbe4e6a9b8d89ed9ff38f5e11.png)
[today in history] July 8: PostgreSQL release; SUSE acquires the largest service provider of k8s; Activision Blizzard merger
![Vulnhub | dc: 6 | [actual combat]](/img/7e/de7d5b56724bde5db2bb8338c35aa8.png)
Vulnhub | dc: 6 | [actual combat]
![[advanced mathematics] [8] differential equation](/img/83/b6b07540e3cf6d6433e57447d42ee9.png)
[advanced mathematics] [8] differential equation

Has baozi ever played in the multi merchant system?

Online random coin tossing positive and negative statistical tool

数据库清空表数据并让主键从1开始

【高等数学】【3】微分中值定理与导数的应用
![[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born](/img/bf/09ccf36caec099098a22f0e8b670bd.png)
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born

增加 swap 空间
随机推荐
JMeter - interface test
[today in history] July 13: the father of database passed away; Apple buys cups code; IBM chip Alliance
文件操作详解
Struct, enum type and union
Aircraft PID control (rotor flight control)
What is cluster analysis? Categories of cluster analysis methods [easy to understand]
Apache MINA框架「建议收藏」
【高等数学】【3】微分中值定理与导数的应用
Redis source code -ziplist
Behind every piece of information you collect, you can't live without TA
飞行器pid控制(旋翼飞控)
Compilation and operation of program
[advanced mathematics] [8] differential equation
JS scope and scope chain
TGA file format (waveform sound file format)
KEGG通路的从属/注释信息如何获取
Solution to oom exceptions caused by improper use of multithreading in production environment (supreme Collection Edition)
数据库清空表数据并让主键从1开始
During the interview, I was asked how to remove the weight of MySQL, and who else wouldn't?
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay