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的更多相关文章

  1. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  2. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  3. Java for LeetCode 146 LRU Cache 【HARD】

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  4. leetcode 146. LRU Cache ----- java

    esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...

  5. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  6. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  7. 【LeetCode】146. LRU Cache

    LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...

  8. 146. LRU Cache (List, HashTable)

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. [LeetCode] 146. LRU Cache 近期最少使用缓存

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

随机推荐

  1. one_day_one_linuxCmd---crontab 命令

    <坚持每天学习一个 linux 命令,今天我们来学习 tar 命令> 摘要:crond 是 linux 下用来周期性的执行某种任务或等待处理事件的一个守护进程,周期执行的任务一般由 cro ...

  2. react动态生成列表

    在组件的render函数中遍历数组menus[]并且要return返回虚拟Dom对象. render() { return createPortal( this.state.visible & ...

  3. JDK的3个bug啊,你get到了吗?

    1.Annotation引用非空enum数组返回空数组 首次发现时的环境:JDK 1.8 首次发现所在项目:APIJSON 测试用例: publicenumRequestRole {/**未登录,不明 ...

  4. springboot跨域请求接口示例

    一.项目架构 二.项目内容 1.GlobalCrosConfig.java package com.config; import org.springframework.context.annotat ...

  5. 剑指offer【08】- 二叉树的深度(java)

    题目:二叉树的深度 考点:知识迁移能力 题目描述:输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 牛客网上的剑指offer题, ...

  6. Scala(一)——scala+Idea环境配置

    Java虚拟机的确是很强大,有很多计算机语言可以运行在虚拟机上,完善了虚拟机上多语言编程. 近年来,大数据云计算,大数据的火爆也让一些小众语言火了起来,如Python,Scala等.这些语言编写简单, ...

  7. UVA 558 SPFA 判断负环

    这个承认自己没看懂题目,一开始以为题意是形成环路之后走一圈不会产生负值就输出,原来就是判断负环,用SPFA很好用,运用队列,在判断负环的时候,用一个数组专门保存某个点的访问次数,超过了N次即可断定有负 ...

  8. vzray上网教程

    1.首先按照之前的教程在chrome里安装插件-Proxy-SwitchyOmega-Chromium-2.5.15 2.打开  vzray-v3.11-windows-64,打开 3.在chrome ...

  9. uni-app: 如何实现增量更新功能?

    都知道,很多APP都有增量更新功能,Uni APP也是在今年初,推出了增量更新功能,今天我们就来学习一波. 当然,很多应用市场为了防止开发者不经市场审核许可,给用户提供违法内容,对增量更新大多持排斥态 ...

  10. 微信小程序-wx.request-路由跳转-数据存储-登录与授权

    wx.request 相当于发送ajax请求 官方文档示例代码 wx.request({ url: 'test.php', //仅为示例,并非真实的接口地址 data: { x: '', y: '' ...