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. MySQL授权用户登录访问指定数据库

    使用Navicat等客户端工具,选中需要共享的数据库,点击查询>新建查询 1.写SQL语句:GRANT ALL PRIVILEGES ON * TO 'test'@'%'IDENTIFIED B ...

  2. HALCON导出函数

    Halcon小函数的封装和代码导出 一.Halcon小函数的封装和修改 1.名词解释: 算子:指Halcon中最基础.最底层的函数(即你看不到它的代码实现),一个算子只有一句话,例如threshold ...

  3. 遍历数组提取List[Int]

    def toFlatMap(input:List[Any],result:List[Int]):List[Int]=input match{ case h::t=>h match {case e ...

  4. Java算法练习——两数之和

    题目链接 题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利 ...

  5. SQLite数据库以及增删改查的案例

    Android使用开源的与操作系统无关的SQL数据库——SQLite 一:在命令行下创建数据库: 1.启动模拟器后,打开命令行,执行adb shell 2.进入所在工程目录 3.执行sqlite3 m ...

  6. Q8:String to Integer (atoi)

    8. String to Integer (atoi) 官方的链接:8. String to Integer (atoi) Description : Implement atoi to conver ...

  7. Java之创建线程的方式三:实现Callable接口

    import java.util.concurrent.Callable;import java.util.concurrent.ExecutionException;import java.util ...

  8. mysql查看整库个表详情

    information_schema.tables字段说明 字段 含义 Table_catalog 数据表登记目录 Table_schema 数据表所属的数据库名 Table_name 表名称 Tab ...

  9. UML-FURPS+与补充性规格说明

    1.FURPS+ 在统一过程(UP)中,需求按照“FURPS+”模型进行分类. 功能性(Functional):特性.功能.安全性: 可用性(Usability):人性化因素.帮助.文档: 可靠性(R ...

  10. UML-如何画通信图?

    1.链 2.消息 3.自身传递消息 4.消息顺序编号 5.有条件消息 6.互斥的有条件消息 7.循环或迭代 8.调用静态方法 9.多态 10.同步和异步调用