https://leetcode.com/problems/lru-cache/

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(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.

class pair {
public int key;
public int value;
public pair(int k, int v) {
super();
this.key = k;
this.value = v;
} public void setValue(int value) {
this.value = value;
} } class cmp implements Comparator { public int compare(Object o1, Object o2) {
pair p1 = (pair) o1;
pair p2 = (pair) o2; if(p1.key < p2.key) {
return -1;
} else if(p1.key == p2.key) {
if(p1.value == p2.value) {
return 0;
} else if(p1.value < p2.value) {
return -1;
} else {
return 1;
}
} else {
return 1;
}
}
}
public class LRUCache { public Set<pair> stack = null;
public HashMap<Integer, Integer> mapping = null;
public TreeMap<Integer, Integer> timeToKey = null;
public TreeMap<Integer, Integer> keyToTime = null;
public int cap = 0;
public int counter = 0; public LRUCache(int capacity) {
this.mapping = new HashMap<Integer, Integer> ();
this.timeToKey = new TreeMap<Integer, Integer> ();
this.keyToTime = new TreeMap<Integer, Integer> ();
this.cap = capacity;
this.counter = 0;
} public int get(int key) { if(!mapping.containsKey(key)) {
return -1;
} else { counter++;
int value = mapping.get(key); int time = keyToTime.get(key);
keyToTime.put(key, counter); timeToKey.remove(time);
timeToKey.put(counter, key); return value;
}
} public void set(int key, int value) { counter++; if(mapping.containsKey(key)) { int time = keyToTime.get(key);
keyToTime.put(key, counter); timeToKey.remove(time);
timeToKey.put(counter, key); mapping.put(key, value); } else { if(mapping.size() < cap) { mapping.put(key, value);
keyToTime.put(key, counter);
timeToKey.put(counter, key); } else { int lru = timeToKey.pollFirstEntry().getValue();
mapping.remove(lru);
mapping.put(key, value); keyToTime.put(key, counter);
timeToKey.put(counter, key);
}
}
}
}

leetcode@ [146] LRU Cache (TreeMap)的更多相关文章

  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 近期最少使用缓存

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

  5. leetcode 146. LRU Cache ----- java

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

  6. Leetcode#146 LRU Cache

    原题地址 以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时.通常大家都是用map+双向链表做的. 我曾经尝试用C++的list容器来写,后来发现map没法保存lis ...

  7. 【LeetCode】146. LRU Cache 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...

  8. 【LeetCode】146. LRU Cache

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

  9. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

随机推荐

  1. 安装 jdk、tomcat

    jdk 下载地址:http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java ...

  2. YTU 2614: A代码完善--系统日期

    2614: A代码完善--系统日期 时间限制: 1 Sec  内存限制: 128 MB 提交: 216  解决: 113 题目描述 注:本题只需要提交填写部分的代码,请按照C++方式提交. 已知某操作 ...

  3. SQLServer2008 行转列3

    with a as ( select 日期,学号,名字, '语文' as 科目,语文 as 分数 from tsco union all select 日期,学号,名字, '数学' as 科目,数学 ...

  4. 计算机术语install和setup的区别

    作为安装程序的Setup文件是软件的开发者专门为其软件设计的.exe文件,是当前最为盛行的安装程序.在运行该Setup文件进行软件安装时,Setup除了进行复制.改名.解压和目录维护等基本安装工作外, ...

  5. leetcode:Palindrome Linked List

    Given a singly linked list, determine if it is a palindrome. Follow up:Could you do it in O(n) time ...

  6. 《OD学HBase》20160820

    一.案例 微博: 微博内容: 关注用户和粉丝用户: 添加或移除关注用户 查看关注用户的微博内容 微博数据存储: 响应时间 秒级 无延迟 (1)mysql分布式 (2)hbase数据库 使用HBase数 ...

  7. javascript高级编程运用

    一//各种尺寸 (size) s += “\r\n网页可见区域宽:“+ document.body.clientWidth; s += “\r\n网页可见区域高:“+ document.body.cl ...

  8. 初步窥探Git

    码农之路恒久远,学习向上是真谛啊!在学习的过程中,相信大家或多或少都接触到Git这个东东.它到底是什么呢,有什么作用呢,为什么它会那么火呢?带着这些一连串的疑问,决心去揭开它的庐山真面目. 在软件开发 ...

  9. jQuery_效果(滑动)

    1.jQuery slideDown() 方法(用于向下滑动元素) 语法:$(selector).slideDown(speed,callback); 可选的 speed 参数规定效果的时长.它可以取 ...

  10. 新手学vim配置

    我是新手啦,以前都没接触过Vim编辑器,所以感觉不怎么顺手,毕竟还没有用习惯.也没有什么基础,所以在配置的时候就一直在网上查资料....想要把vim编辑器配置成VS的话可以参考这个:http://ww ...