当前位置:网站首页>LeetCode 146. LRU Cache
LeetCode 146. LRU Cache
2022-08-10 12:00:00 【Mizuna pen】
原题网址:https://leetcode.cn/problems/lru-cache/
Fixed-capacity cache,If full,Add new ones again,The least recently used node will be deleted
要求是get,put都是O(1),
class LRUCache {
// 用map保存数据;Guaranteed access isO(1)
// Doubly linked lists preserve order.
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;
}
}
边栏推荐
- LeetCode 83. Remove Duplicate Elements in Sorted List
- Licking Exercise - 59 From Binary Search Trees to Greater Sum Trees
- Licking Exercise - 63 Find all anagrams in a string
- 即时零售业态下如何实现自动做账?
- LeetCode 19. Delete the Nth last node of the linked list
- 有哪些好用的性能测试工具推荐?性能测试报告收费标准
- jlink and swd interface definition
- Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
- LeetCode 445. 两数相加 II
- 做自媒体月入几万?博主们都在用的几个自媒体工具
猜你喜欢

VSCode remote connection server error: Could not establish connection to "xxxxxx" possible error reasons and solutions

dedecms支持Word内容一键导入

ViT结构详解(附pytorch代码)

嘉为蓝鲸荣获工信部“数字技术融合创新应用解决方案”

AutoCAD Map 3D功能之一暴力处理悬挂点(延伸)

学长告诉我,大厂MySQL都是通过SSH连接的

Go 事,Gopher 要学的数字类型,变量,常量,运算符 ,第2篇

微信小程序,全局变量一个地方改变了其他地方的状态也跟着改变。

态路小课堂丨如何为CXP光模块选择光纤跳线?

LeetCode50天刷题计划(Day 19—— 在排序数组中查找元素的第一个和最后一个位置(9.10-10.40)
随机推荐
Clicking Exercise - 64 Longest Harmonic Subsequences
Samsung plans to start producing semiconductor components in Vietnam in 2023
网络基础(第一节)
LeetCode 237. 删除链表中的节点
VSCode远程连接服务器报错:Could not establish connection to “xxxxxx”的可能错误原因及解决
LeetCode 82. 删除排序链表中的重复元素 II
力扣练习——63 找到字符串中所有字母异位词
配置swagger
项目部署、
常量及数据类型你还记得多少?
电脑怎么设置屏幕息屏时间(日常使用分享)
单目操作符(含原码反码补码转换)
HDU 4372:Count the Buildings (Stirling数)
快手“弃”有赞与微盟“结亲”,电商SaaS行业竞争格局将变?
The author of open source also has a life problem
LeetCode 24. Swap nodes in linked list pairwise
力扣练习——60 二叉搜索子树的最大键值和
网络套接字(UDP和TCP编程)
LeetCode 362. Design Hit Counter
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常