当前位置:网站首页>LeetCode 146. LRU 缓存
LeetCode 146. LRU 缓存
2022-08-10 11:09:00 【水菜笔】
原题网址:https://leetcode.cn/problems/lru-cache/
固定容量的缓存,如果满了后,再次添加新的,会删除最近最少使用的那个节点
要求是get,put都是O(1),
class LRUCache {
// 用map保存数据;保证存取是O(1)
// 双向链表保存顺序。
ListNode head;
ListNode tail;
Map<Integer, ListNode> data;
int capacity;
public LRUCache(int capacity) {
data = new HashMap<>();
this.capacity = capacity;
}
private void removeNode(ListNode cur) {
// 维护顺序
ListNode next = cur.next;
ListNode prev = cur.prev;
if(prev != null) {
prev.next = next;
cur.prev = null;
}else {
// 说明删除的是头结点
head = next;
// cur.next = null;
}
if(next != null){
next.prev = prev;
cur.next = null;
}else {
// 删除的是尾结点
tail = prev;
}
}
private void insertHead(ListNode cur) {
cur.next = head;
if(head != null) {
head.prev = cur;
head = cur;
} else {
head = tail = cur;
}
}
public int get(int key) {
ListNode cur = data.get(key);
// 有值,则返回数据
if(cur != null) {
// 维护顺序
removeNode(cur);
insertHead(cur);
return cur.value;
}
return -1;
}
public void put(int key, int value) {
ListNode node = data.get(key);
if(node != null) {
node.value = value;
removeNode(node);
insertHead(node);
} else {
if(data.size()>= capacity) {
node = tail;
removeNode(tail);
data.remove(node.key);
}
node = new ListNode(key,value);
data.put(key,node);
insertHead(node);
}
}
}
class ListNode {
int key;
int value;
ListNode prev;
ListNode next;
public ListNode(int key,int value) {
this.key = key;
this.value = value;
}
}
边栏推荐
- 中小规模网站架构
- The brave rice rice, does not fear the brush list of 】 list has a ring
- 接口定义与实现
- Article take you understand interrupt the key driver of polling mechanism
- Introduction to Software Architecture
- 【勇敢饭饭,不怕刷题之链表】链表倒数节点问题
- LeetCode_443_压缩字符串
- 阻塞 非阻塞 poll机制 异步
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- Module 9 - Designing an e-commerce seckill system
猜你喜欢
AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)
即时零售业态下如何实现自动做账?
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
【勇敢饭饭,不怕刷题之链表】链表反转的几种情况
Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
rider内Mono脚本找不到引用资源
Spss-多元回归案例实操
Some tips for using Unsafe
石墨文档打开文档时快速定位到上次写的位置
VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions
随机推荐
Centos7环境使用Mysql离线安装包安装Mysql5.7
Buckle Exercise - 61 Sort by frequency of characters
WeChat applet, global variables change in one place and the state in other places also changes.
从源码角度分析UUID的实现原理
codevs 2370 小机房的树 (LCA)
Hangdian Multi-School-Loop-(uncertainty greedy + line segment tree)
HDU 4135:Co-prime (容斥原理)
10 个 Reduce 常用“奇技淫巧”
学长告诉我,大厂MySQL都是通过SSH连接的
力扣练习——58 验证二叉搜索树
LeetCode_152_乘积最大子数组
如何使用工程仪器设备在线监测管理系统
【小程序 | 启航篇】一文打通任督二脉
Introduction to Software Architecture
How to join We Media, learn about these 5 monetization modes, and make your account quickly monetize
Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇
LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
AUTOCAD - reducing spline curve control points, the advanced CAD practice (3)
codevs 2370 Small room tree (LCA)
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)