当前位置:网站首页>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);
*/
边栏推荐
- 数据准备工作
- Audio and video engineer YUV and RGB detailed explanation
- Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
- 剑指 Offer 30. 包含min函数的栈
- Global and Chinese market of commercial cheese crushers 2022-2028: Research Report on technology, participants, trends, market size and share
- 事故指标统计
- Template_ Quick sort_ Double pointer
- 【机器人库】 awesome-robotics-libraries
- 微软语音合成助手 v1.3 文本转语音工具,真实语音AI生成器
- 一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
猜你喜欢
Prepare for the autumn face-to-face test questions
【无标题】数据库中一条查询SQL执行的过程
Spark accumulator
一位博士在华为的22年
Minecraft 1.16.5 biochemical 8 module version 2.0 storybook + more guns
[robot library] awesome robots Libraries
米家、涂鸦、Hilink、智汀等生态哪家强?5大主流智能品牌分析
Audio and video engineer YUV and RGB detailed explanation
Pat grade a 1033 to fill or not to fill
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
随机推荐
Minecraft 1.18.1、1.18.2模组开发 22.狙击枪(Sniper Rifle)
Advanced technology management - what is the physical, mental and mental strength of managers
2345文件粉碎,文件强力删除工具无捆绑纯净提取版
Blue Bridge Cup group B provincial preliminaries first question 2013 (Gauss Diary)
一个复制也能玩出花来
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
Compact lidar global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
数据准备工作
2022年版图解网络PDF
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 16
729. My schedule I / offer II 106 Bipartite graph
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 lethal serial question 1 -- are you familiar with MySQL transactions?
SSM 程序集
MySQL winter vacation self-study 2022 11 (8)
论文笔记: 极限多标签学习 GalaXC (暂存, 还没学完)
Pat grade a 1033 to fill or not to fill
vs code保存时 出现两次格式化
Overview of spark RDD