当前位置:网站首页>Leetcode linked list queue stack problem
Leetcode linked list queue stack problem
2022-06-11 01:39:00 【Black white grey 12345】
Linked list
Double linked list
146. LRU cache
- 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 .Link
- Realize the idea
- get function
- Hash map not found : return -1
- Hash map found : Delete this node from the linked list 、 Move this node to the head of the linked list 、 Returns the value of the node
- put function
- Hash map not found :
- The cache is full : Delete the end node of the linked list , Create a new node , Insert the head of the linked list , Update hash map
- Cache not full : Create a new node , Insert the head of the linked list , Update hash map
- Merge : When the cache is full, delete the end of the linked list node , Then create a new one and insert it into the head , Update hash map
- Hash map found : Move the node to the head , Update the value value
- Hash map not found :
- Bidirectional linked list is used for caching , Hash mapping is used to find
- Borrow existing list Container and iterator realize the function of bidirectional linked list
- list The underlying time of is based on a two-way linked list
- Delete the keywords in the cache 、 Insert keyword into header ; Keyword advance is realized
- After cache operation, pay attention to Update hash map
class LRUCache {
public:
LRUCache(int capacity) : cap(capacity){
}
int get(int key) {
if (hashtable.find(key) == hashtable.end()) return -1;
auto key_value = *hashtable[key]; // * iterator, Take out list The pair in pair Value
// Delete the cache first , Then insert into list The first , Implement recently used
cache.erase(hashtable[key]);
cache.push_front(key_value);
hashtable[key] = cache.begin(); // Update hash map
return key_value.second;
}
void put(int key, int value) {
// If the keyword is not in the cache
if (hashtable.find(key) == hashtable.end()) {
if (hashtable.size() == cap) {
// If the cache is full
// Delete the last cache , Clear its hash map
hashtable.erase(cache.back().first);
cache.pop_back();
}
}else {
cache.erase(hashtable[key]); // Keywords in cache
}
cache.push_front({
key, value}); // Add keywords to the cache header , And update the hash map
hashtable[key] = cache.begin();
}
private:
int cap;
list<pair<int,int>> cache;
unordered_map<int, list<pair<int,int>>::iterator> hashtable;
};
Manual implementation of two-way linked list
Established key、 Mapping of node pointers , Therefore, the hash map does not need to be updated after the node is moved
First remove the node from the linked list 、 Then move to the head of the linked list
Remove nodes from the linked list node

The nodes node Move to the head of the list

class LRUCache {
public:
struct DLinkNode {
int key, value;
DLinkNode *prev, *next;
DLinkNode (){
};
DLinkNode(int _key, int _value) : key(_key), value(_value), prev(nullptr), next(nullptr) {
};
};
LRUCache(int capacity) : cap(capacity){
// Create header 、 The last two nodes are easy to find
head = new DLinkNode(-1, -1);
tail = new DLinkNode(-1, -1);
head->next = tail;
tail->prev = head;
}
int get(int key) {
if (hash.find(key) == hash.end()) return -1;
DLinkNode* node = hash[key];
// Remove this node from its original location , Then move to the head
removeNode(node); // It's very important
moveHead(node);
return node->value;
}
void put(int key, int value) {
if (hash.find(key) == hash.end()) {
if (hash.size() == cap) {
DLinkNode *node = removeTail();
// Delete last node , And clear the hash map
hash.erase(node->key);
delete node;
}
// Add nodes and create hash maps
addHead(key, value);
hash[key] = head->next;
}else {
DLinkNode* node = hash[key];
node->value = value;
// Delete this node from its original position and move it to the head
removeNode(node);
moveHead(node);
}
}
void moveHead(DLinkNode* node) {
DLinkNode *nextNode;
nextNode = head->next;
// Insert the current node into head and head In the middle of the next node of , Head
node->prev = head;
node->next = nextNode;
nextNode->prev = node;
head->next = node;
}
void addHead(int key, int value) {
// Create a new node , And move it to the head of the linked list
DLinkNode *node = new DLinkNode(key, value);
moveHead(node);
}
void removeNode(DLinkNode* node) {
// Remove the current node from the linked list
DLinkNode *prevN = node->prev;
DLinkNode *nextN = node->next;
prevN->next = nextN;
nextN->prev = prevN;
}
DLinkNode* removeTail() {
DLinkNode* node = tail->prev; // The tail node of the linked list
removeNode(node); // Remove this node from the linked list
return node; // return
}
private:
int cap;
DLinkNode *head;
DLinkNode *tail;
unordered_map<int, DLinkNode*> hash;
};
边栏推荐
- threejs:如何获得几何体的boundingBox?
- Using completabilefuture
- Yunna Qingyuan fixed assets management and barcode inventory system
- [interpretation of the paper] sort out the papers on the vision based autonomous landing platform of UAV
- Introduction to the policy support of Beijing China Patent Award, with a subsidy of 1million yuan
- Shenzhen Nanshan District specialized, special and new enterprise application conditions, with a subsidy of 100000-500000 yuan
- Application of object storage S3 in distributed file system
- What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
- 复利的保险理财产品怎么样?可以买吗?
- Millions of visits - resolution of high concurrency problems
猜你喜欢

记录打包GoogleChrome浏览器插件

Understanding of multithreading

焱融看|混合云环境下,如何实现数据湖最优存储解决方案

Web3 ecological decentralized financial platform sealem Finance

Docking of express bird system

项目_基于网络爬虫的疫情数据可视化分析

Implementing MySQL fuzzy search with node and express

PX4装机教程(六)垂起固定翼(倾转)

There is a problem with numpy after CONDA installs pytoch

Inventory management and strategy mode
随机推荐
[geometric vision] 4.2 piecewise linear transformation
How to download web photos
Support standard for cultivation of high-tech enterprises in Changping District, Beijing, with a subsidy of 100000 yuan
懒汉式单例模式
深圳中国专利奖政策支持介绍,补贴100万
北京房山区高新技术企业培育支持标准,补贴10万
CSRF攻击
SAS cluster analysis (system cluster, dynamic cluster fastclus, variable cluster varclus)
Beijing Mentougou District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
Brief description of custom annotations
如何使用自定义注解进行参数校验
Shenzhen Nanshan District specialized special new enterprise application process, with a subsidy of RMB 100000-500000
中间件_Redis_06_Redis的事务
Introduction to the policy support of Shenzhen China Patent Award, with a subsidy of 1million yuan
Once you know these treasure websites, you can't live without them!!!
CSRF attack
nodejs中使用mySql数据库
Beijing Yanqing District high tech enterprise cultivation support standard, with a subsidy of 100000 yuan
I was so excited about the college entrance examination in 2022
2.2、ROS+PX4仿真多点巡航飞行----正方形