当前位置:网站首页>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);
*/边栏推荐
- 一位博士在华为的22年
- Minecraft 1.16.5 生化8 模组 2.0版本 故事书+更多枪械
- Number conclusion LC skimming review - 1
- After changing the GCC version, make[1] appears in the compilation: cc: command not found
- Y a - t - il des cas où sqlcdc surveille plusieurs tables et les associe à une autre? Tout fonctionne dans MySQL
- [coppeliasim] 6-DOF path planning
- Reset nodejs of the system
- HDU_ p1237_ Simple calculator_ stack
- 【MySQL 15】Could not increase number of max_ open_ files to more than 10000 (request: 65535)
- Compact lidar global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
猜你喜欢

Structural theme model (I) STM package workflow

Keyword static

在线怎么生成富文本

构建库函数的雏形——参照野火的手册
![[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8](/img/16/33f5623625ba817e6e022b5cb7ff5d.jpg)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8

爬虫(9) - Scrapy框架(1) | Scrapy 异步网络爬虫框架

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

技术管理进阶——什么是管理者之体力、脑力、心力

数据工程系列精讲(第四讲): Data-centric AI 之样本工程

Black high-end responsive website dream weaving template (adaptive mobile terminal)
随机推荐
Looking at the trend of sequence modeling of recommended systems in 2022 from the top paper
好用的 JS 脚本
Data preparation
PAT甲级 1033 To Fill or Not to Fill
Zero foundation self-study STM32 - Review 2 - encapsulating GPIO registers with structures
Global and Chinese markets of general purpose centrifuges 2022-2028: Research Report on technology, participants, trends, market size and share
Blue Bridge Cup group B provincial preliminaries first question 2013 (Gauss Diary)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 17
数据准备工作
从顶会论文看2022年推荐系统序列建模的趋势
Six stone management: why should leaders ignore product quality
零基础自学STM32-复习篇2——使用结构体封装GPIO寄存器
Paper notes: graph neural network gat
The third level of C language punch in
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
事故指标统计
A doctor's 22 years in Huawei
Method of changing object properties
Dachang image library
2020.02.11