当前位置:网站首页>Leetcode-146: LRU cache
Leetcode-146: LRU cache
2022-07-25 20:37:00 【Chrysanthemum headed bat】
leetcode-146:LRU cache
subject
Please design and implement a meeting LRU ( Recently at least use ) cache Constrained data structure .
Realization LRUCache class :
- LRUCache(int capacity) With Positive integer As capacity capacity initialization LRU cache
- int get(int key) If the keyword key In the cache , The value of the keyword is returned , Otherwise return to -1 .
- void put(int key, int value) If the keyword key Already exist , Change its data value value ; If it doesn't exist , Then insert the group into the cache key-value . If the insert operation causes the number of keywords to exceed capacity , Should be Eviction The longest unused keyword .
function get and put Must be O(1) The average time complexity of running .
Example :
Input
["LRUCache", "put", "put", "get", "put", "get", "put", "get", "get", "get"]
[[2], [1, 1], [2, 2], [1], [3, 3], [2], [4, 4], [1], [3], [4]]
Output
[null, null, null, 1, null, -1, null, -1, 3, 4]
explain
LRUCache lRUCache = new LRUCache(2);
lRUCache.put(1, 1); // The cache is {1=1}
lRUCache.put(2, 2); // The cache is {1=1, 2=2}
lRUCache.get(1); // return 1
lRUCache.put(3, 3); // This operation causes the keyword to 2 To void , The cache is {1=1, 3=3}
lRUCache.get(2); // return -1 ( Not found )
lRUCache.put(4, 4); // This operation causes the keyword to 1 To void , The cache is {4=4, 3=3}
lRUCache.get(1); // return -1 ( Not found )
lRUCache.get(3); // return 3
lRUCache.get(4); // return 4
Problem solving
Method 1 : Double linked list + Hash
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)){
// If key There is , Then modify the corresponding value
Node* p=hash[key];
p->value=value;
remove(p);
insert(p);
}else{
if(hash.size()==n){
// If the cache is full , Delete the rightmost node of the double linked list
Node* p=R->left;
remove(p);
hash.erase(p->key);// Update hash table
delete p;
}
// Insert a new node
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;
}
};
边栏推荐
- The uniapp project starts with an error binding Node is not a valid Win32 Application ultimate solution
- [MCU] 51 MCU burning those things
- "Chain" connects infinite possibilities: digital asset chain, wonderful coming soon!
- 数据库清空表数据并让主键从1开始
- Compilation and operation of program
- [onnx] export pytorch model to onnx format: support multi parameter and dynamic input
- Do you still have certificates to participate in the open source community?
- 网络爬虫原理解析「建议收藏」
- 【NOI模拟赛】字符串匹配(后缀自动机SAM,莫队,分块)
- How to choose a microservice registration center?
猜你喜欢

Card link

Chinese son-in-law OTA Ono became the first Asian president of the University of Michigan, with an annual salary of more than 6.5 million!
![[advanced mathematics] [1] function, limit, continuity](/img/c5/f9fd3814a61d0fba24b37253c7e51c.png)
[advanced mathematics] [1] function, limit, continuity

leetcode-919:完全二叉树插入器

【高等数学】【1】函数、极限、连续
![Vulnhub | dc: 5 | [actual combat]](/img/c6/34117bbfb83ebdf9e619f4e4590661.png)
Vulnhub | dc: 5 | [actual combat]

智能电子界桩自然保护区远程监控解决方案

Do you still have certificates to participate in the open source community?
![[advanced mathematics] [4] indefinite integral](/img/4f/2aae654599fcc0ee85cb1ba46c9afd.png)
[advanced mathematics] [4] indefinite integral

Increase swap space
随机推荐
C language file reading and writing
什么是聚类分析?聚类分析方法的类别[通俗易懂]
[today in history] June 29: SGI and MIPS merged; Microsoft acquires PowerPoint developer; News corporation sells MySpace
TGA file format (waveform sound file format)
Clickhouse notes 02 -- installation test clickvisual
[today in history] July 1: the father of time-sharing system was born; Alipay launched barcode payment; The first TV advertisement in the world
Chapter VI modified specification (SPEC) class
Step num problem
RF, gbdt, xgboost feature selection methods "recommended collection"
Socket error Event: 32 Error: 10053. Connection closing...Socket close
leetcode-79:单词搜索
Introduction and construction of consul Registration Center
How to choose a microservice registration center?
The database empties the table data and makes the primary key start from 1
Apache Mina framework "suggestions collection"
Difference Between Accuracy and Precision
Technology cloud report: more than zero trust, the wild hope of Parra's "Digital Security Cloud strategy"
Brush questions with binary tree (4)
【TensorRT】trtexec工具转engine
每条你收藏的资讯背后,都离不开TA