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. MTK6577 Android源代码目录

    MTK6577 Android源代码目录 1.     MTKAndroid4.0 源代码目录 (1)  makeMtk 整个工程编译或是构建(make/build)的入口. (2)  abi 应用程 ...

  2. grep/awk/sed 或者 并且 否定

    Grep 'OR' Operator Find all the lines in a file, that match any of the following patterns. Using GRE ...

  3. visual studio 中删除多余的空白行

    替换  Ctrl+H  正则  勾选  替换 ^\s*\n 为空

  4. tc srm 632 500 (规律)

    We have a sequence of N positive integers: a[0] through a[N-1]. You do not know these integers. All ...

  5. jQuery_效果(淡入淡出)

    1.jQuery fadeIn() 方法( 用于淡入已隐藏的元素.) 语法: $(selector).fadeIn(speed,callback); 可选的 speed 参数规定效果的时长.它可以取以 ...

  6. Machine Learning for hackers读书笔记_一句很重要的话

    为了培养一个机器学习领域专家那样的直觉,最好的办法就是,对你遇到的每一个机器学习问题,把所有的算法试个遍,直到有一天,你凭直觉就知道某些算法行不通.

  7. ionic2rc版常见的一些坑

    1.config.xml里的包名不能有横杠,否则在build android的时候会报错 <widget id="com.ionicframework.name-abc" v ...

  8. MVC简捷调用EasyUI的datagrid

    一直想在项目中使用EasyUi的datagrid,但种种原因,没有实现. 这两天在开发一个项目中,愿望终于得以实现. 先看效果: 实现步骤是这样的: 1,在页面中画dataGrid,具体代码如下: & ...

  9. linux安全体系

    一.   硬件.软件选型 确认需求,分析可能面临的安全问题 记录各硬件寿命.保证散热.确保性能冗余 使用商业正版.开源的软件,不从互联网安装系统,从源头保证系统安全 购买专业售后支持 服务器配置双电源 ...

  10. BZOJ 1406 密码箱

    直接两层枚举就行了. 避免排序可以用set. #include<iostream> #include<cstdio> #include<cstring> #incl ...