[LC] 146. LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.
get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.put(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.
The cache is initialized with a positive capacity.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
LRUCache cache = new LRUCache( 2 /* capacity */ ); cache.put(1, 1);
cache.put(2, 2);
cache.get(1); // returns 1
cache.put(3, 3); // evicts key 2
cache.get(2); // returns -1 (not found)
cache.put(4, 4); // evicts key 1
cache.get(1); // returns -1 (not found)
cache.get(3); // returns 3
cache.get(4); // returns 4
class LRUCache {
private int capacity;
private Map<Integer, Node> map;
private Node head;
private Node tail;
// hash map makes O(1) to get and put
// doubly LL makes removing easier
public LRUCache(int capacity) {
this.map = new HashMap<>();
this.capacity = capacity;
this.head = null;
this.tail = null;
}
public int get(int key) {
Node curNode = map.get(key);
if (curNode == null) {
return -1;
}
if (tail != curNode) {
if (curNode == head) {
head = head.next;
} else {
curNode.prev.next = curNode.next;
curNode.next.prev = curNode.prev;
}
tail.next = curNode;
curNode.prev = tail;
tail = curNode;
}
return curNode.value;
}
public void put(int key, int value) {
Node curNode = map.get(key);
if (curNode != null) {
// update the current Node, for get(), similiar with this snippet
curNode.value = value;
if (tail != curNode) {
if (curNode == head) {
head = head.next;
} else {
curNode.prev.next = curNode.next;
curNode.next.prev = curNode.prev;
}
tail.next = curNode;
curNode.prev = tail;
tail = curNode;
}
} else {
Node newNode = new Node(key, value);
if (capacity == 0) {
Node tmp = head;
head = tmp.next;
map.remove(tmp.key);
capacity += 1;
}
if (head == null && tail == null) {
head = newNode;
} else {
tail.next = newNode;
newNode.prev = tail;
}
tail = newNode;
capacity -= 1;
map.put(key, newNode);
}
}
}
class Node {
int key;
int value;
Node prev;
Node next;
public Node(int key, int value) {
this.key = key;
this.value = value;
}
}
/**
* Your LRUCache object will be instantiated and called as such:
* LRUCache obj = new LRUCache(capacity);
* int param_1 = obj.get(key);
* obj.put(key,value);
*/
[LC] 146. LRU Cache的更多相关文章
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- 146. LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 【LeetCode】146. LRU Cache
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...
- 146. LRU Cache (List, HashTable)
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- Nginx系列p4:进程结构
Nginx 有两种进程结构:单进程结构,多进程结构.本篇文章我们主要说多进程结构. 问:那为什么 Nginx 采用多进程结构,而不是多线程结构呢? 答:这是因为 Nginx 最核心的目的就是要保证高可 ...
- LeJOS下eclipse通过USB连接EV3
适用情况:在eclipse安装好LeJOS插件后,再通过USB连接EV3. 第一:更新驱动 在<设备管理器-网络适配器>下更新RNDIS/Ethernet Gadget,电脑需要先连接EV ...
- UML-异常处理
1.名词解释 缺陷(Fault):错误引起的行为.如:程序员拼写错了数据库名称 错误(Error):缺陷在运行系统中的表现.如:当使用拼写错误的名称调用数据库时,抛出数据库异常 故障(Failure) ...
- 连接mysql的各种方式
mysql连接操作是客户端进程与mysql数据库实例进程进行通信.从程序设计角度来说,属于进程通信,常用进程通信包括: 管道.Tcp/Ip 套接字.UNIX域套接字. 1.TCP/IP (1)使用最多 ...
- Maven:A cycle was detected in the build path of project 'xxx'. The cycle consists of projects {xx}
以下这个错误是在Eclipse中导入多个相互依赖的工程时出现的“循环依赖问题”:A cycle was detected in the build path of project 'xxx'. The ...
- JFrame的面板结构和JPanel的使用
JFrame图解结构 有一窗口框架实例:JFrame win = new JFrame("窗口");在new JFrame()时,构建了JFrame实例对象,在实例中的Layere ...
- 2014_csu选拔1_B
Description Here is no naked girl nor naked runners, but a naked problem: you are to find the K-th s ...
- 批量导入数据表(oracle)
批量导入数据表(oracle) 1.登陆plsql 2.找到菜单栏 工具>>导入数据>>新增图标(会提示选择*.csv文件) 选择如上图所示 3.选择数据并导入 4.下图为执行 ...
- update-help : 无法更新带有 UI 区域性 {zh-CN} 的模块“WindowsUpdateProvider”帮助: 在 HelpInfo XML 文件中检索不到 UI 区域性 zh-CN
环境 OS: Windows10 企业版 LTSC x64 CPU: Intel i5-7500 CPU 3.4GHz PowerShell:5.1.17763.503 描述 更新powershell ...
- PAT B1045 快速排序
题目如下: 1045 快速排序 (25 point(s)) 著名的快速排序算法里有一个经典的划分过程:我们通常采用某种方法取一个元素作为主元,通过交换,把比主元小的元素放到它的左边,比主元大的元素放到 ...