当前位置:网站首页>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;
}
}
边栏推荐
- Interviewer: How are Dao, Service, Controller, Util, and Model divided in the project?
- A little self-deprecating deconstruction about farmers "code"
- mysql出现:ERROR 1524 (HY000): Plugin ‘123‘ is not loaded
- HDU 6040 Hints of sd0061 (技巧)
- 英特尔推送20220809 CPU微码更新 修补Intel-SA-00657安全漏洞
- 皕杰报表在传参乱码
- 模块九 - 设计电商秒杀系统
- Buckle exercise - rectangular area does not exceed the maximum value of K and (hard)
- 【Untitled】
- Since the media hot style title how to write?Taught you how to write the title
猜你喜欢

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

Emulate stm32 directly with proteus - the programmer can be completely discarded

即时零售业态下如何实现自动做账?

Analysis of the implementation principle of UUID from the perspective of source code

石墨文档打开文档时快速定位到上次写的位置

std::move()
今天面了个腾讯拿38K出来的大佬,让我见识到了基础的天花板

网络基础(第一节)

APP automation testing practice based on UiAutomator2+PageObject mode

基于UiAutomator2+PageObject模式开展APP自动化测试实战
随机推荐
HDU 1520 Anniversary party (tree dp)
从脚本到剪辑,影像大师亲授的后期制作秘籍
codevs 2370 Small room tree (LCA)
网络套接字(UDP和TCP编程)
OPNsense安装配置Zenarmor
2022年裁员潮,失业程序员何去何从?
Licking Exercise - 60 Maximum key-value sum of binary search subtrees
【勇敢饭饭,不怕刷题之链表】链表中有环的问题
Analysis of the name matching process between the LCD driver and the device (Tiny4412)
mysql appears: ERROR 1524 (HY000): Plugin '123' is not loaded
怎么加入自媒体,了解这5种变现模式,让账号快速变现
不止跑路,拯救误操作rm -rf /*的小伙儿
单目操作符(含原码反码补码转换)
Weilai-software development engineer side record
Since the media hot style title how to write?Taught you how to write the title
Programmers pursue technology to consolidate basic learning route suggestions
Emulate stm32 directly with proteus - the programmer can be completely discarded
基于UiAutomator2+PageObject模式开展APP自动化测试实战
LeetCode 82. 删除排序链表中的重复元素 II
力扣练习——62 有效的数独