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. 设置Apache(httpd)和Nginx 开机自启动

     方法1: 进入目录: vi  /etc/rc.d/rc.local #设置apache 和 nginx 开机自启动/usr/sbin/apachectl start/usr/sbin/nginx s ...

  2. idea排除要编译的文件

    感觉应该有更好的方式.VS中可以右键文件从项目中排除 如果要恢复选中要恢复的文件,点击 +下的-即可

  3. Aspectj切入点语法定义

    例如定义切入点表达式  execution (* com.sample.service.impl..*.*(..)) execution()是最常用的切点函数,其语法如下所示: 整个表达式可以分为五个 ...

  4. Arduino IIC 主从设备连接通信

    目的:        实现Arduino主从设备之间的互相IIC通信,掌握IIC通信协议的使用方法. 器材: Arduino UNO R3 一块 Arduino Nano 三块 面包板   导线 3K ...

  5. BZOJ 3170 [Tjoi2013]松鼠聚会

    题解:切比雪夫距离转化为曼哈顿距离 枚举源点,横纵坐标互不影响,分开考虑,前缀和优化 横纵分开考虑是一种解题思路 #include<iostream> #include<cstdio ...

  6. SDWebImage清理缓存

    [[SDImageCache sharedImageCache] getSize]//计算缓存的大小,单位B float tmpSize = [[SDImageCache sharedImageCac ...

  7. 汪慧和201771010123《面向对象程序设计(Java)》第三周学习总结

    1.实验目的与要求 (1)进一步掌握Eclipse集成开发环境下java程序开发基本步骤: (2)熟悉PTA平台线上测试环境: (3)掌握Java语言构造基本程序语法知识(ch1-ch3): (4)利 ...

  8. Python对象赋值、浅拷贝、深拷贝

    Python中,基本数据类型,理解为常见数据类型:布尔型.整型.浮点型.字符串.列表.元组.字典.集合,随语言不同而不同,但是根据在内存中存储方式的不同,区分开原子类型和容器类型. 对象赋值 对象的赋 ...

  9. 基于libcurl的GET与POST(HTTP1.1)

    #include <stdio.h> #include <curl/curl.h> bool getUrl(char *filename) { CURL *curl; CURL ...

  10. MongoDB_走一波

    Mongodb 一.mongodb的介绍 mongodb的优势 易扩展:NoSQL数据库种类繁多,但是一个共同的特定就是去掉关系数据库的关系型特性.数据之间无关系,这样非常容易扩展 大数据,高性能:N ...