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. k8s yaml 文件中字段类型:

    1.<Object>    对象类型 metadata: name: namespace: 2.<[]Object>  对象列表类型 containers: -  name: ...

  2. 吴裕雄--天生自然MySQL学习笔记:MySQL WHERE 子句

    MySQL 表中使用 SQL SELECT 语句来读取数据. 如需有条件地从表中选取数据,可将 WHERE 子句添加到 SELECT 语句中. 语法 以下是 SQL SELECT 语句使用 WHERE ...

  3. 【转帖】使用了 Service Mesh 后我还需要 API 网关吗?

    使用了 Service Mesh 后我还需要 API 网关吗? https://www.kubernetes.org.cn/6762.html api gateway和istio 是不一样的 追求不一 ...

  4. CentOS8上用Docker部署开源项目Tcloud

    一.安装Docker 1.我是虚拟机装的Centos7,linux 3.10 内核,docker官方说至少3.8以上,建议3.10以上(ubuntu下要linux内核3.8以上) root账户登录,查 ...

  5. GTX 1080显卡出错

    NVRM: RmInitAdapter failed! (0x26:0xffff:1097) NVRM: rm_init_adapter failed for device bearing minor ...

  6. L0,L1,L2正则化浅析

    在机器学习的概念中,我们经常听到L0,L1,L2正则化,本文对这几种正则化做简单总结. 1.概念 L0正则化的值是模型参数中非零参数的个数. L1正则化表示各个参数绝对值之和. L2正则化标识各个参数 ...

  7. 吴裕雄--天生自然 PHP开发学习:表单 - 验证邮件和URL

    $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $ ...

  8. build模式入门,build模式理解(转载)

    问:为何要用? 普通做法: 1.创建pojo public class Person { private String name; private int age; private double he ...

  9. PAT甲级——1061 Dating (20分)

    Sherlock Holmes received a note with some strange strings: Let's date! 3485djDkxh4hhGE 2984akDfkkkkg ...

  10. Java类只加载一次的情况

    一个类只加载一次: 调用Java命令. 创建对象时 访问静态成员时 Class.forName("包名.类名")