当前位置:网站首页>【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;
}
};
边栏推荐
- NPM install is stuck with various strange errors of node NPY
- Exercise 10-1 judge the three digits that meet the conditions
- Exercise 6-6 use a function to output an integer in reverse order
- Sendmail can't send mail and it's too slow to send. Solve it
- 泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
- Luogu p5536 [xr-3] core city solution
- Table of mathematical constants by q779
- 数学常数表 by q779
- Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
- 7-11 calculation of residential water charges by sections
猜你喜欢
7-18 finding the single root of polynomial by dichotomy
分布式事务(Seata) 四大模式详解
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Niuke: crossing the river
Mysql多表查询 #子查询
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules
Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder
protobuf与grpc
Doris学习笔记之数据表的创建
随机推荐
LNMP环境mail函数不能发送邮件解决
ConstraintLayout 的使用
puzzle(016.3)千丝万缕
C language,%d% Difference between 2D%2d%02d
China PETG market forecast and Strategic Research Report (2022 Edition)
7-9 find a small ball with a balance
556. 下一个更大元素 III
Add ZABBIX calculation type itemcalculated items
Statistical capital consonants
Understand the application scenario and implementation mechanism of differential segment
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
NPM install is stuck with various strange errors of node NPY
7-11 calculation of residential water charges by sections
Tonybot Humanoïde Robot Infrared Remote play 0630
Thread.sleep和TimeUnit.SECONDS.sleep的区别
Understanding of closures
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Solr series of full-text search engines - basic principles of full-text search
7-16 find the set of integers that meet the given conditions
Programming language: the essence of type system