当前位置:网站首页>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;
}
};
边栏推荐
- 文件操作详解
- Rand1 generates rand9
- How to obtain the subordinate / annotation information of KEGG channel
- Timing analysis and constraints based on xlinx (1) -- what is timing analysis? What are temporal constraints? What is temporal convergence?
- 【高等数学】【1】函数、极限、连续
- MySQL date [plus sign / +] condition filtering problem
- Aircraft PID control (rotor flight control)
- 2022.7.24-----leetcode.1184
- Fanoutexchange switch code tutorial
- How to use buffer queue to realize high concurrent order business (glory Collection Edition)
猜你喜欢

Docker builds redis cluster

Prescan quick start to master the special functions of prescan track editing in lecture 18

Cloud native, Intel arch and cloud native secret computing three sig online sharing! See you today | issues 32-34

Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
![[tensorrt] dynamic batch reasoning](/img/59/42ed0074de7162887bfe2c81891e20.png)
[tensorrt] dynamic batch reasoning

从底层结构开始学习FPGA(16)----PLL/MMCM IP的定制与测试
![[advanced mathematics] [3] Application of differential mean value theorem and derivative](/img/a9/3b024dbbb201bee4eed6c9f6ce3001.png)
[advanced mathematics] [3] Application of differential mean value theorem and derivative

Has baozi ever played in the multi merchant system?

Volcanic engine Xiang Liang: machine learning and intelligent recommendation platform multi cloud deployment solution officially released

Interpretation of filter execution sequence source code in sprigboot
随机推荐
Myormframeworkjdbc review and problem analysis of user-defined persistence layer framework, and thought analysis of user-defined persistence layer framework
【TensorRT】trtexec工具转engine
Learn FPGA from the bottom structure (16) -- customization and testing of pll/mmcm IP
[advanced mathematics] [4] indefinite integral
Why did I choose to become a network engineer after graduating from weak current for 3 months
Apache MINA框架「建议收藏」
Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
Scan delete folder problems
qml 结合 QSqlTableModel 动态加载数据 MVC「建议收藏」
JMeter - interface test
Today's sleep quality record 75 points
Kubernetes进阶部分学习笔记
Has baozi ever played in the multi merchant system?
Go language go language built-in container
Unity VS—— VS中默认调试为启动而不是附加到Unity调试
[advanced drawing of single cell] 07. Display of KEGG enrichment results
Array of sword finger offer question bank summary (I) (C language version)
Vulnhub | dc: 5 | [actual combat]
“链”接无限可能:数字资产链,精彩马上来!
移动web布局方法