LeetCode – LRU Cache (Java)
Problem
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.
Java Solution
The key to solve this problem is using a double linked list which enables us to quickly move nodes.

import java.util.HashMap;
public class LRUCache {
private HashMap<Integer, DoubleLinkedListNode> map
= new HashMap<Integer, DoubleLinkedListNode>();
private DoubleLinkedListNode head;
private DoubleLinkedListNode end;
private int capacity;
private int len;
public LRUCache(int capacity) {
this.capacity = capacity;
len = 0;
}
public int get(int key) {
if (map.containsKey(key)) {
DoubleLinkedListNode latest = map.get(key);
removeNode(latest);
setHead(latest);
return latest.val;
} else {
return -1;
}
}
public void removeNode(DoubleLinkedListNode node) {
DoubleLinkedListNode cur = node;
DoubleLinkedListNode pre = cur.pre;
DoubleLinkedListNode post = cur.next;
if (pre != null) {
pre.next = post;
} else {
head = post;
}
if (post != null) {
post.pre = pre;
} else {
end = pre;
}
}
public void setHead(DoubleLinkedListNode node) {
node.next = head;
node.pre = null;
if (head != null) {
head.pre = node;
}
head = node;
if (end == null) {
end = node;
}
}
public void set(int key, int value) {
if (map.containsKey(key)) {
DoubleLinkedListNode oldNode = map.get(key);
oldNode.val = value;
removeNode(oldNode);
setHead(oldNode);
} else {
DoubleLinkedListNode newNode =
new DoubleLinkedListNode(key, value);
if (len < capacity) {
setHead(newNode);
map.put(key, newNode);
len++;
} else {
map.remove(end.key);
end = end.pre;
if (end != null) {
end.next = null;
}
setHead(newNode);
map.put(key, newNode);
}
}
}
}
class DoubleLinkedListNode {
public int val;
public int key;
public DoubleLinkedListNode pre;
public DoubleLinkedListNode next;
public DoubleLinkedListNode(int key, int value) {
val = value;
this.key = key;
}
}
ps:
存在并发问题。
LeetCode – LRU Cache (Java)的更多相关文章
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LeetCode——LRU Cache
Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- Leetcode: LRU Cache 解题报告
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LeetCode: LRU Cache [146]
[题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...
- [LeetCode] LRU Cache [Forward]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LRU Cache java实现
要求: get(key):如果key在cache中,则返回对应的value值,否则返回null set(key,value):如果key不在cache中,则将该(key,value)插入cache中( ...
随机推荐
- SQL Server导入导出表及备份恢复
1. 导出: 2. 导入
- AE2
2.3缩放属性 ctrl+D 复制图层:这样可以吧这个图层的所有属性都复制过去 然后按着alt键,把图片替换掉 需要做一个倒计时(作业)(到1时做一个烟花效果) 问题:1.按着alt键,把图片替换掉. ...
- PAT-7-14 电话聊天狂人
ps: 真不明白为什么水题不能一次ac 7-14 电话聊天狂人(25 分) 给定大量手机用户通话记录,找出其中通话次数最多的聊天狂人. 输入格式: 输入首先给出正整数N(≤105),为通话记录条 ...
- HPU第三次积分赛-D:Longest Increasing Subsequence(DP)
Longest Increasing Subsequence 描述 给出一组长度为n的序列,a1,a2,a3,a4...an, 求出这个序列长度为k的严格递增子序列的个数 输入 第一行输入T ...
- C++学习(二十六)(C语言部分)之 结构体3(联合,枚举)
结构体 struct 类型定义点运算符 . 变量名.成员 成员是数组的时候不能用等于号赋值箭头运算符 -> 指针->成员 作用 存放多个不同类型的有关联的数据 与结构体类似的类型1.联合作 ...
- 【传输协议】TCP、IP协议族之数字签名与HTTPS详解
文章转载出自:https://blog.51cto.com/11883699/2160032 安全的获取公钥 细心的人可能已经注意到了如果使用非对称加密算法,我们的客户端A,B需要一开始就持有公钥,要 ...
- 利用 httpmodule 强制所有页面使用同一基类
public class OMSPageChecker : IHttpModule { public void Dispose() { } public void Init(HttpApplicati ...
- 浮动IP(Floating IPs):开始构建你的高可用性的应用
高可用性是所有生产环境的关键.开发者因此可以高枕无忧因为他们知道他们的应用被设计为可以承受住故障. 今天,我们非常激动的宣布我们应用了浮动IP技术.浮动IP指的是一个IP地址可以立即从一个Drople ...
- Django中需要注意的点
需要注意的点 请求相关 注销的 用法 def logout(request): request.session.flush()#输入此内容可以注销用户登录信息 # 即将session信息清除掉 ret ...
- 初始Openwrt
系统结构 在上一章我们已经完成了刷机工作,这个时候系统进行了首次启动,并且格式化了它的"可写"分区.那么在设备里分区到底是怎么样进行的呢?我们首先需要知道:不同的处理器下OpenW ...