当前位置:网站首页>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;
}
}
边栏推荐
- Ssm framework construction process [easy to understand]
- LeetCode 362. Design Hit Counter(计数器)
- HDU 4372:Count the Buildings (Stirling数)
- LeetCode 362. Design Hit Counter
- Article take you understand interrupt the key driver of polling mechanism
- 电脑怎么设置屏幕息屏时间(日常使用分享)
- LeetCode 19. Delete the Nth last node of the linked list
- LeetCode50天刷题计划(Day 16—— 两两交换链表中的节点(9.10-10.30)
- LeetCode 82. Remove Duplicate Elements in Sorted List II
- Licking Exercise - 58 Verifying Binary Search Trees
猜你喜欢

从源码角度分析UUID的实现原理

LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)

Nocalhost - Making development more efficient in the cloud-native era

3款不同类型的自媒体免费工具,有效提高创作、运营效率

CPU多级缓存与缓存一致性

负载均衡原理分析与源码解读

StoneDB Document Bug Hunting Season 1

Since the media hot style title how to write?Taught you how to write the title

WeChat applet, global variables change in one place and the state in other places also changes.

Flutter气泡框实现
随机推荐
制品库是什么?
力扣练习——60 二叉搜索子树的最大键值和
【mysql】explain介绍[通俗易懂]
力扣练习——58 验证二叉搜索树
力扣练习——61 根据字符出现频率排序
LeetCode 24. 两两交换链表中的节点
力扣练习——64 最长和谐子序列
个推数据资产管理经验 | 教你打造数据质量心电图,智能检测数据“心跳”异常
力扣练习—— 矩形区域不超过 K 的最大数值和(hard)
LeetCode50天刷题计划(Day 18—— 搜索旋转排序数组(8.50-12.00)
力扣练习——59 从二叉搜索树到更大和树
三星计划2023年开始在越南生产半导体零部件
HDU 4372:Count the Buildings (Stirling数)
Analysis of the implementation principle of UUID from the perspective of source code
Apple bucks the trend and expands iPhone 14 series stocking, with a total of 95 million units
3款不同类型的自媒体免费工具,有效提高创作、运营效率
LeetCode 445. Adding Two Numbers II
LeetCode 362. Design Hit Counter
LeetCode 24. Swap nodes in linked list pairwise
Flutter气泡框实现