当前位置:网站首页>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;
}
};
边栏推荐
- Docker 搭建 Redis Cluster集群
- Docker builds redis cluster
- Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34
- How to obtain the subordinate / annotation information of KEGG channel
- 【高等数学】【8】微分方程
- Scan delete folder problems
- “链”接无限可能:数字资产链,精彩马上来!
- test
- Chapter VI modified specification (SPEC) class
- The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
猜你喜欢
![[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

Has baozi ever played in the multi merchant system?
![[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference](/img/0f/8ce2d5487b16d38a152cfd3ab454bb.png)
[today in history] July 17: Softbank acquired arm; The first email interruption; Wikimedia International Conference
![[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

Mobile web layout method

LeetCode通关:哈希表六连,这个还真有点简单

Interpretation of filter execution sequence source code in sprigboot

Introduction and construction of consul Registration Center

MySQL 日期【加号/+】条件筛选问题

103. (cesium chapter) cesium honeycomb diagram (square)
随机推荐
preprocessor directives
Step num problem
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
Proxy实现mysql读写分离
【高等数学】【3】微分中值定理与导数的应用
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
Interpretation of filter execution sequence source code in sprigboot
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
毕业从事弱电3个月,我为什么会选择转行网络工程师
Clickhouse notes 02 -- installation test clickvisual
ROS_ Rqt toolbox
Stock software development
JMeter - interface test
Cloud native guide: what is cloud native infrastructure
Technology cloud report: what is the difference between zero trust and SASE? The answer is not really important
增加 swap 空间
Network RTK UAV test [easy to understand]
[today in history] June 28: musk was born; Microsoft launched office 365; The inventor of Chua's circuit was born
Vivo official website app full model UI adaptation scheme
[today in history] July 3: ergonomic standards act; The birth of pioneers in the field of consumer electronics; Ubisoft releases uplay