当前位置:网站首页>【7.3】146. LRU caching mechanism
【7.3】146. LRU caching mechanism
2022-07-03 14:32:00 【howtoloveyou】
Method : Hashtable + Double linked list
- keyword :key,value Yes
- pattern recognition : Once a key value pair appears , Think of hash table
- Change the access time of data :1 Need to be able to access randomly ,2 You need to be able to insert the data into the head or tail
test :
- Test insert , Including spillage
- Test update , Check for correct updates
- Test read , Check whether it can be read correctly
hashmap+ Double linked list , Complexity analysis :
- Time complexity :O1, Hash table location O1, List operation O1
- Spatial complexity :Oc
Code :
struct DLinkedNode {
int key, value;
DLinkedNode* prev;
DLinkedNode* next;
DLinkedNode(): key(0), value(0), prev(nullptr), next(nullptr) {
}
DLinkedNode(int _key, int _value): key(_key), value(_value), prev(nullptr), next(nullptr) {
}
};
class LRUCache {
private:
unordered_map<int, DLinkedNode*> cache;
DLinkedNode* head;
DLinkedNode* tail;
int size;
int capacity;
public:
LRUCache(int _capacity): capacity(_capacity), size(0) {
While initializing the data structure in the code , initialization capacity and size Two parameters
// Using pseudo head and pseudo tail nodes
head = new DLinkedNode(); Create a head node
tail = new DLinkedNode(); Create tail node
head->next = tail; Head -》 The tail
tail->prev = head; Head 《- The tail
}
int get(int key) {
if (!cache.count(key)) {
If not, return -1
return -1;
}
// If key There is , First, locate through the hash table , And then to the head
DLinkedNode* node = cache[key]; Locate the node through the hash table and then operate on it
moveToHead(node); Here, the header is represented as the most frequently used element
return node->value;
}
void put(int key, int value) {
if (!cache.count(key)) {
// If key non-existent , Create a new node
DLinkedNode* node = new DLinkedNode(key, value);
// Add to hash table
cache[key] = node;
// Add to the head of the bidirectional list
addToHead(node);
++size;
if (size > capacity) {
// If the capacity is exceeded , Delete the tail node of the bidirectional linked list
DLinkedNode* removed = removeTail();
// Delete the corresponding entry in the hash table
cache.erase(removed->key);
// Prevent memory leaks
delete removed;
--size;
}
}
else {
// If key There is , First, locate through the hash table , Revise value, And move to the head
DLinkedNode* node = cache[key];
node->value = value;
moveToHead(node);
}
}
void addToHead(DLinkedNode* node) {
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
void removeNode(DLinkedNode* node) {
node->prev->next = node->next;
node->next->prev = node->prev;
}
void moveToHead(DLinkedNode* node) {
removeNode(node);
addToHead(node);
}
DLinkedNode* removeTail() {
Here we extract the nodes that have not been used recently , It will not affect the operation of deleting him in the hash table , Because he didn't disappear
DLinkedNode* node = tail->prev;
removeNode(node);
return node;
}
};
边栏推荐
- Thread.sleep和TimeUnit.SECONDS.sleep的区别
- tonybot 人形机器人 首次开机 0630
- Preliminary summary of structure
- 7-28 monkeys choose King (Joseph problem)
- Common shortcut keys in PCB
- Tonybot Humanoïde Robot Infrared Remote play 0630
- 牛客网:过河卒
- Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
- 7-18 finding the single root of polynomial by dichotomy
- Sword finger offer 28 Symmetric binary tree
猜你喜欢

天图投资冲刺港股:资产管理规模249亿 投了小红书与奈雪

Four data flows and cases of grpc

Programming language: the essence of type system

ConstraintLayout 的使用

NPM install is stuck with various strange errors of node NPY

Exercise 6-1 classify and count the number of characters

MySQL multi table query subquery

亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
![[clean up the extraordinary image of Disk C]](/img/0d/331115bc5d82d6337ae975a08494b2.jpg)
[clean up the extraordinary image of Disk C]

Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
随机推荐
US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
Common shortcut keys in PCB
Comprehensive evaluation of good-looking, easy-to-use and powerful handwriting note taking software: notability, goodnotes, marginnote, handwriting, notes writers, collanote, collanote, prodrafts, not
Although not necessarily the best, it must be the hardest!
Leetcode (4) -- find the median of two positively ordered arrays
Sendmail can't send mail and it's too slow to send. Solve it
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
基因家族特征分析 - 染色体定位分析
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Doris学习笔记之数据表的创建
retrofit
String reverse order
Too many files with unapproved license
分布式事务(Seata) 四大模式详解
7-15 calculation of PI
Understand the application scenario and implementation mechanism of differential segment
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
一文了解微分段应用场景与实现机制
Exercise 6-6 use a function to output an integer in reverse order
编程语言:类型系统的本质