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中( ...
随机推荐
- Gym.102059: 2018-2019 XIX Open Cup, Grand Prix of Korea(寒假gym自训第一场)
整体来说,这一场的质量比较高,但是题意也有些难懂. E.Electronic Circuit 题意: 给你N个点,M根线,问它是否是一个合法的电路. 思路: 一个合法的电路,经过一些串联并联关系, ...
- JAVA:形参与实参
今天百度startWith函数的用法,无意中看到了形参这个称呼,因此就去了解了下形参与实参. 在传值机制中,其实就是把变量b(实参)的地址传递给了形参(也就是实参跟形参都是用的同一个地址,在传值之前形 ...
- NYOJ 12:喷水装置(二)(贪心,区间覆盖问题)
12-喷水装置(二) 内存限制:64MB 时间限制:3000ms 特判: No 通过数:28 提交数:109 难度:4 题目描述: 有一块草坪,横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n ...
- windows apache "The requested operation has failed" 启动失败
找到失败原因,进入cmd(win+r快捷键,输入cmd)命令行下 进入到你的apache bin目录下: 每个人错误可能不同,根据自己问题去相应改
- C++学习(三)(C语言部分)之 基本数据类型
基本数据类型 上期回顾 stdlib.h system,命令release MT导入ico文件 基本数据类型 整数 int浮点型(小数 实型) float double字符型 char 变量 常量速度 ...
- quicklink 基本使用
原理 使用可见性以及预取数据,同时充分利用浏览器的空闲时间,主要是解析href 以通过代码的选项指定需要加载的数据,当然其中 也添加了好多灵活的控制参数,方便我们使用,而且代码很小,压缩之后也就1kb ...
- oracle-null和默认值
Oracle的默认值处理要当心,如果应用中使用的是ORM工具,则必须要考虑对于字段为Null的处理,必要时在ORM工具中将Null转换为default或插入时去掉值为Null的字段. 可以将下面的系统 ...
- 最大值最小值(max,max_element)
min 如果比不出大小就返回第一个引数 //版本一:调用operator< template <class LessThanComparable> const LessThanCom ...
- 数学集合:N Z Q R C
整数: Zahlen(德) 复数: Complex number 实数: Real number 自然数: Natural number 有理数: Quotient(德,"商&quo ...
- pyspark数据准备
鸢尾花数据集 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3.1,1 ...