当前位置:网站首页>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);
*/
边栏推荐
- Building the prototype of library functions -- refer to the manual of wildfire
- Li Kou today's question -729 My schedule I
- Data preparation
- 2022.02.13
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
- HDU_ p1237_ Simple calculator_ stack
- Adapter-a technology of adaptive pre training continuous learning
- Shell脚本更新存储过程到数据库
- MySQL winter vacation self-study 2022 11 (6)
- 零基础自学STM32-野火——GPIO复习篇——使用绝对地址操作GPIO
猜你喜欢
2022 edition illustrated network pdf
从顶会论文看2022年推荐系统序列建模的趋势
Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux
剑指 Offer 29. 顺时针打印矩阵
Initial understanding of pointer variables
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
一个复制也能玩出花来
The third level of C language punch in
Formatting occurs twice when vs code is saved
UE4 - how to make a simple TPS role (I) - create a basic role
随机推荐
Easy to use js script
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 7
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)
Global and Chinese markets of nasal oxygen tubes 2022-2028: Research Report on technology, participants, trends, market size and share
How to generate rich text online
inherited constructors
ftp上传文件时出现 550 Permission denied,不是用户权限问题
Bigder: I felt good about the 34/100 interview, but I didn't receive the admission
Pat grade a 1033 to fill or not to fill
[community personas] exclusive interview with Ma Longwei: the wheel is not easy to use, so make it yourself!
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
[untitled] a query SQL execution process in the database
力扣今日题-729. 我的日程安排表 I
MySQL winter vacation self-study 2022 11 (8)
【coppeliasim】6自由度路径规划
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
Redis installation
sql表名作为参数传递
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 18