当前位置:网站首页>LeetCode146. LRU cache
LeetCode146. LRU cache
2022-07-05 02:47:00 【C-position debut_ two thousand and twenty-two】
LRU Full name Least Recently Used, The least used recently , The first time I heard this concept was in the operating system course of my junior , For page replacement algorithm , For virtual page storage management services .
LRU The proposed algorithm is based on a fact , Pages that are frequently used in the previous instructions are likely to be frequently used in the next few instructions , Conversely, if you haven't used it for a long time, you think you won't use it next , This is the principle of locality
Now? LRU Algorithms are also often used as cache obsolescence strategies .
LeetCode146. LRU Cache is such a problem , It is said that this is also a common point of interview
In fact, if you understand the principle and use the right method, it is not difficult
Obviously, this cache is in order , Eliminate cache from the end of the bidirectional linked list , Add and get Methods put the node at the head , It's not difficult to follow the code thinking
public class LRUCache {
private Map<Integer, MyLinkedNode> cache = new HashMap<Integer, MyLinkedNode>();
private int capacity;// Initialize capacity
private int size;// Cache existing quantity
private MyLinkedNode head,tail;
public LRUCache(int capacity) {
this.size = 0;
this.capacity = capacity;
head = new MyLinkedNode();
tail = new MyLinkedNode();
head.next = tail;
tail.prev = head;
}
public int get(int key) {
MyLinkedNode node = cache.get(key);
if(node == null){
return -1;
}
moveToHead(node);// Every time get Just put the node into the head , It means using more
return node.value;
}
public void put(int key, int value) {
MyLinkedNode node = cache.get(key);
if(node == null){// If this node does not exist, create one and add it to the cache
MyLinkedNode newNode = new MyLinkedNode(key,value);
cache.put(key,newNode);
addToHead(newNode);
++size;
if(size > capacity){
// If the capacity exceeds, delete the tail node of the linked list
MyLinkedNode tail = deleteTail();
cache.remove(tail.key);// According to this key Index delete cache The key value in it is right
--size;
}
}else {// This key If it exists , Just modify value And move to the head
node.value = value;
moveToHead(node);
}
}
private void addToHead(MyLinkedNode node){// Add nodes to the chain header
node.prev = head;
node.next = head.next;
head.next.prev = node;
head.next = node;
}
private void moveToHead(MyLinkedNode node){// Transfer the nodes in the linked list to the head
removeNode(node);
addToHead(node);
}
private void removeNode(MyLinkedNode node){// Delete node
node.prev.next = node.next;
node.next.prev = node.prev;
}
private MyLinkedNode deleteTail(){// Delete tail node
MyLinkedNode res = tail.prev;
removeNode(res);
return res;
}
class MyLinkedNode{// Two-way list node
int key;
int value;
MyLinkedNode prev;
MyLinkedNode next;
public MyLinkedNode(){}
public MyLinkedNode(int key,int value){
this.key = key;
this.value = value;
}
}
public static void main(String[] args) {
LRUCache cache = new LRUCache(2);
cache.put(1,1);
cache.put(2, 2);
System.out.println(cache.get(1));
cache.put(3, 3);
System.out.println(cache.get(2));
cache.put(4, 4);
System.out.println(cache.get(1));
System.out.println(cache.get(3));
System.out.println(cache.get(4));
}
}
When I read the comment section of this topic, someone said , Why does the customized node have key This attribute ,cache Variables are not based on key Did you save it , There is only one in the node value Is it not good?
without key If so, we need to delete the tail node when we eliminate the cache , Note that the node we deleted only dereferences the nodes in the bidirectional linked list , But this node is map Type of cache It's still real , If we don't have any nodes key What to use as an index to delete cache The key value in it is right . This is the maintenance in the user-defined linked list node key The key
边栏推荐
- Introduce reflow & repaint, and how to optimize it?
- Design of kindergarten real-time monitoring and control system
- Acwing第 58 场周赛【完结】
- 【LeetCode】110. Balanced binary tree (2 brushes of wrong questions)
- openresty ngx_lua執行階段
- Design and implementation of campus epidemic prevention and control system based on SSM
- Utilisation simple de devtools
- Linux安装Redis
- Exploration of short text analysis in the field of medical and health (II)
- Blue bridge - maximum common divisor and minimum common multiple
猜你喜欢
2.常见的请求方法
单项框 复选框
2021 Li Hongyi machine learning (3): what if neural network training fails
Marubeni Baidu applet detailed configuration tutorial, approved.
Tencent cloud, realize image upload
问题解决:AttributeError: ‘NoneType‘ object has no attribute ‘append‘
Elfk deployment
Watch the online press conference of tdengine community heroes and listen to TD hero talk about the legend of developers
Azkaban实战
Hmi-30- [motion mode] the module on the right side of the instrument starts to write
随机推荐
Pytest (5) - assertion
为什么腾讯阿里等互联网大厂诞生的好产品越来越少?
[Yu Yue education] National Open University spring 2019 0505-22t basic nursing reference questions
Day_ 17 IO stream file class
From task Run get return value - getting return value from task Run
2021 Li Hongyi machine learning (2): pytorch
openresty ngx_ Lua execution phase
The phenomenology of crypto world: Pioneer entropy
openresty ngx_ Lua variable operation
Vb+access hotel service management system
This + closure + scope interview question
Kotlin - coroutine
Sqoop命令
Spoon inserts and updates the Oracle database, and some prompts are inserted with errors. Assertion botch: negative time
Good documentation
Tiny series rendering tutorial
Port, domain name, protocol.
Android advanced interview question record in 2022
[Yu Yue education] National Open University autumn 2018 8109-22t (1) monetary and banking reference questions
Redis distributed lock, lock code logic