当前位置:网站首页>Force buckle 146 LRU cache
Force buckle 146 LRU cache
2022-07-06 02:32:00 【Yangshiwei....】
subject :
analysis :
You can add a two-way linked list to the hash table ,size Record the amount of data in the current cache ,capacity The maximum amount of data that can be stored in the cache , It is also the input parameter brought when initializing the class ,first,last The two nodes represent the head and tail of the bidirectional linked list respectively , Insert data using tail interpolation , The previous node of the tail node is the most recently used node , The next node of the head node is the most rarely used node , That is to say LRU The one replaced in the algorithm ,hash Responsible for indexing , If there is this index value in the hash , Then he is in the cache , Otherwise, it's not in .
Code :
class LRUCache {
class Node{
int key;
int val;
Node next;
Node pre;
public Node(){}
public Node(int key,int val){
this.key=key;
this.val=val;
this.next=null;
this.pre=null;
}
}
HashMap<Integer,Node> hash=new HashMap<Integer,Node>();
int size;
int capacity;
Node first;
Node last;
public LRUCache(int capacity) {
this.size=0;
this.capacity=capacity;
this.first=new Node();
this.last=new Node();
first.next=last;
last.pre=first;
}
public int get(int key) {
Node n=hash.get(key);
if(n!=null){
Node pre=n.pre;
Node nex=n.next;
if(nex!=last){
nex.pre=pre;
pre.next=nex;
n.pre=last.pre;
last.pre.next=n;
n.next=last;
last.pre=n;
}
return n.val;
}else{
return -1;
}
}
public void put(int key, int value) {
Node n=hash.get(key);
if(n!=null){
Node pre=n.pre;
Node nex=n.next;
if(nex!=last){
nex.pre=pre;
pre.next=nex;
n.pre=last.pre;
last.pre.next=n;
n.next=last;
last.pre=n;
}
n.val=value;
}else{
if(size==capacity){
int k=first.next.key;
hash.remove(k);
first.next=first.next.next;
first.next.pre=first;
size--;
}
n=new Node(key,value);
n.pre=last.pre;
last.pre.next=n;
n.next=last;
last.pre=n;
hash.put(key,n);
size++;
}
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
边栏推荐
- [robot library] awesome robots Libraries
- 【机器人库】 awesome-robotics-libraries
- Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
- Li Kou today's question -729 My schedule I
- 【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
- [robot hand eye calibration] eye in hand
- 更改对象属性的方法
- How to generate rich text online
- 【社区人物志】专访马龙伟:轮子不好用,那就自己造!
- 2022.02.13
猜你喜欢
2022 eye health exhibition, vision rehabilitation exhibition, optometry equipment exhibition, eye care products exhibition, eye mask Exhibition
【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
Shell脚本更新存储过程到数据库
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
【MySQL 15】Could not increase number of max_open_files to more than 10000 (request: 65535)
Formatting occurs twice when vs code is saved
Pat grade a 1033 to fill or not to fill
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework
Minecraft 1.16.5 biochemical 8 module version 2.0 storybook + more guns
Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
随机推荐
High number_ Vector algebra_ Unit vector_ Angle between vector and coordinate axis
Sword finger offer 30 Stack containing min function
[robot hand eye calibration] eye in hand
LeetCode 103. Binary tree zigzag level order transverse - Binary Tree Series Question 5
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
Thinking on Architecture Design (under continuous updating)
550 permission denied occurs when FTP uploads files, which is not a user permission problem
Overview of spark RDD
Global and Chinese market of commercial cheese crushers 2022-2028: Research Report on technology, participants, trends, market size and share
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
MySQL winter vacation self-study 2022 11 (9)
Y a - t - il des cas où sqlcdc surveille plusieurs tables et les associe à une autre? Tout fonctionne dans MySQL
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 16
Shell脚本更新存储过程到数据库
[coppeliasim] efficient conveyor belt
Easy to use js script
Li Kou today's question -729 My schedule I
Lecture 4 of Data Engineering Series: sample engineering of data centric AI
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18