当前位置:网站首页>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);
*/边栏推荐
- Use Scrollview and tabhost to realize vertical scrollbars and tabs
- 米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析
- Accident index statistics
- Structural theme model (I) STM package workflow
- Zero basic self-study STM32 wildfire review of GPIO use absolute address to operate GPIO
- Initial understanding of pointer variables
- Dachang image library
- Building the prototype of library functions -- refer to the manual of wildfire
- MySQL winter vacation self-study 2022 11 (6)
- 一位博士在华为的22年
猜你喜欢

【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)

Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I

Httprunnermanager installation (III) - configuring myql Database & initialization data under Linux

【无标题】数据库中一条查询SQL执行的过程
![[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning](/img/37/83953c24ec141667b304f9dac440f1.jpg)
[Wu Enda machine learning] week5 programming assignment EX4 - neural network learning

Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework

Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper

Advanced technology management - what is the physical, mental and mental strength of managers

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

How to generate rich text online
随机推荐
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
2022年版图解网络PDF
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
2022 edition illustrated network pdf
Reset nodejs of the system
I changed the driver to 5.1.35, but it is still the same error. I can succeed even now, but I will report this every time I do an SQL operation
机器学习训练与参数优化的一般过程 (讨论)
Data preparation
RDD creation method of spark
GifCam v7.0 极简GIF动画录制工具中文单文件版
构建库函数的雏形——参照野火的手册
MySQL lethal serial question 1 -- are you familiar with MySQL transactions?
sql表名作为参数传递
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 9
Déduisez la question d'aujourd'hui - 729. Mon emploi du temps I
Structural theme model (I) STM package workflow
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
Global and Chinese markets of screw rotor pumps 2022-2028: Research Report on technology, participants, trends, market size and share
Crawler (9) - scrape framework (1) | scrape asynchronous web crawler framework