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.

  1. class pair {
  2. public int key;
  3. public int value;
  4. public pair(int k, int v) {
  5. super();
  6. this.key = k;
  7. this.value = v;
  8. }
  9.  
  10. public void setValue(int value) {
  11. this.value = value;
  12. }
  13.  
  14. }
  15.  
  16. class cmp implements Comparator {
  17.  
  18. public int compare(Object o1, Object o2) {
  19. pair p1 = (pair) o1;
  20. pair p2 = (pair) o2;
  21.  
  22. if(p1.key < p2.key) {
  23. return -1;
  24. } else if(p1.key == p2.key) {
  25. if(p1.value == p2.value) {
  26. return 0;
  27. } else if(p1.value < p2.value) {
  28. return -1;
  29. } else {
  30. return 1;
  31. }
  32. } else {
  33. return 1;
  34. }
  35. }
  36. }
  37. public class LRUCache {
  38.  
  39. public Set<pair> stack = null;
  40. public HashMap<Integer, Integer> mapping = null;
  41. public TreeMap<Integer, Integer> timeToKey = null;
  42. public TreeMap<Integer, Integer> keyToTime = null;
  43. public int cap = 0;
  44. public int counter = 0;
  45.  
  46. public LRUCache(int capacity) {
  47. this.mapping = new HashMap<Integer, Integer> ();
  48. this.timeToKey = new TreeMap<Integer, Integer> ();
  49. this.keyToTime = new TreeMap<Integer, Integer> ();
  50. this.cap = capacity;
  51. this.counter = 0;
  52. }
  53.  
  54. public int get(int key) {
  55.  
  56. if(!mapping.containsKey(key)) {
  57. return -1;
  58. } else {
  59.  
  60. counter++;
  61. int value = mapping.get(key);
  62.  
  63. int time = keyToTime.get(key);
  64. keyToTime.put(key, counter);
  65.  
  66. timeToKey.remove(time);
  67. timeToKey.put(counter, key);
  68.  
  69. return value;
  70. }
  71. }
  72.  
  73. public void set(int key, int value) {
  74.  
  75. counter++;
  76.  
  77. if(mapping.containsKey(key)) {
  78.  
  79. int time = keyToTime.get(key);
  80. keyToTime.put(key, counter);
  81.  
  82. timeToKey.remove(time);
  83. timeToKey.put(counter, key);
  84.  
  85. mapping.put(key, value);
  86.  
  87. } else {
  88.  
  89. if(mapping.size() < cap) {
  90.  
  91. mapping.put(key, value);
  92. keyToTime.put(key, counter);
  93. timeToKey.put(counter, key);
  94.  
  95. } else {
  96.  
  97. int lru = timeToKey.pollFirstEntry().getValue();
  98. mapping.remove(lru);
  99. mapping.put(key, value);
  100.  
  101. keyToTime.put(key, counter);
  102. timeToKey.put(counter, key);
  103. }
  104. }
  105. }
  106. }

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. [NYIST737]石子合并(一)(区间dp)

    题目链接:http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=737 很经典的区间dp,发现没有写过题解.最近被hihocoder上几道比赛题难住了 ...

  2. leetcode:Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1即反转二叉树,代码如下: /** * Defin ...

  3. 【转】Android横竖屏切换问题

    Android横竖屏切换总结(Android资料) Android横竖屏要解决的问题应该就两个: 一.布局问题 二.重新载入问题 1.布局问题:如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的 ...

  4. HDFS的基本shell操作,hadoop fs操作命令

    (1)分布式文件系统 随着数据量越来越多,在一个操作系统管辖的范围存不下了,那么就分配到更多的操作系统管理的磁盘中,但是不方便管理和维护,因此迫切需要一种系统来管理多台机器上的文件,这就是分布式文件管 ...

  5. 【Android】 PopupWindow使用小结

        PopupWindow的很多用法网上比较多,我就不做过多解释了,只说下可能会遇到的问题,以及解决办法: 1.PopupWindow中的listview无响应 这个主要是因为show写在了set ...

  6. github概念和实战

    fork: 通过fork操作,你将拥有了别人创建的repo的ownership,但是url却变成了/youraccount/repo,这时你将可以做git push操作 clone: 该命令是直接将r ...

  7. core--多线程

    WINDOWS是一个多线程操作系统,所谓多线程,就是在同一时间里,有多个线程同时在运行.我们上一遍说到CPU的执行序列是严格按照顺序来执行,怎么能够同一时间来执行很多程序呢?在早期答案是:window ...

  8. 如何使用SQL Server链接服务器访问DB2 Server

    首先,需要安装Microsoft OLE DB Provider for DB2 下载地址:http://download.microsoft.com/download/B/B/2/BB22098A- ...

  9. Bitset位图

    位图(bitmap)就是用每一位来存放某种状态,适合于大规模数据但是数据状态又不是很多的情况下,通常来判断数据是否存在.位图的常见应用有两种: 1.存放大规模数据,例如腾讯的面试题,给40亿个unsi ...

  10. WEBUS2.0 In Action - 索引操作指南(2)

    上一篇:WEBUS2.0 In Action - 索引操作指南(1) | 下一篇:WEBUS2.0 In Action - 搜索操作指南(1) 3. 添加.删除.撤销删除和修改文档 在WEBUS中要将 ...