当前位置:网站首页>【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;
}
};
边栏推荐
- etcd集群权限管理和账号密码使用
- 中国PETG市场预测及战略研究报告(2022版)
- Exercise 6-1 classify and count the number of characters
- Mysql多表查询 #子查询
- Concat and concat_ Ws() differences and groups_ Use of concat() and repeat() functions
- Puzzle (016.4) domino effect
- C language,%d% Difference between 2D%2d%02d
- 556. The next larger element III
- Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
- 7-20 print 99 formula table (format output)
猜你喜欢
Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
Exercise 10-1 judge the three digits that meet the conditions
Exercise 8-8 moving letters
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
Exercise 9-3 plane vector addition
Happy capital new dual currency fund nearly 4billion yuan completed its first account closing
Programming language: the essence of type system
Exercise 10-8 recursive implementation of sequential output of integers
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
Puzzle (016.4) domino effect
随机推荐
7-6 mixed type data format input
7-23 currency conversion (using array conversion)
Common commands for getting started with mongodb database
Zabbix添加Calculated items后保存页面成空白
String sort
How to query the baby category of tmall on Taobao
如何查询淘宝天猫的宝贝类目
Convert string to decimal integer
Find specified characters
Exercise 10-3 recursive implementation of exponential functions
protobuf与grpc
Sendmail无法发送邮件及发送过慢解决
X86 assembly language - Notes from real mode to protected mode
Why is this error reported when modifying records in the database
556. The next larger element III
puzzle(016.4)多米诺效应
Tonybot Humanoïde Robot Infrared Remote play 0630
PCB中常用快捷键
7-10 calculate salary
Thread. Sleep and timeunit SECONDS. The difference between sleep