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. 51 nod 1006 最长公共子序列Lcs

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1006 参考博客 :http://blog.csdn.net/yysdsy ...

  2. leetcode:Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  3. Toad

    1. Toad 规矩: toad 不会违反, 限制, 扩大 你当前用户的权限, toad 不会影响你定义的关于instance的内容. 2. toad 可以执行大部分在 sql*plus 中执行的命令 ...

  4. 【转载】关于XML文档的xmlns、xmlns:xsi和xsi:schemaLocation

    原文在: https://yq.aliyun.com/articles/40353 这里有转载:http://www.cnblogs.com/zhao1949/p/5652167.html 先来一段S ...

  5. POJ 2689

    题意:求[l, r]区间中的间隔距离最大与最小的相邻两个素数,r<2200000000, r-l<10^6 题解: 对于<a的合数,其必然存在一个素因子b<=sqrt(a). ...

  6. SVN功能详解

    SVN功能详解   TortoiseSVN是windows下其中一个非常优秀的SVN客户端工具.通过使用它,我们可以可视化的管理我们的版本库.不过由于它只是一个客户端,所以它不能对版本库进行权限管理. ...

  7. [转][TFS] 禁止默认允许多人签出和强制解除签入签出锁

    转自:http://blog.xieyc.com/tfs-disable-multiple-check-out-and-force-to-undo-locking/ | 小谢的小站 [TFS] 禁止默 ...

  8. (转载)C语言预处理

    C程序的源代码中可包括各种编译指令,这些指令称为预处理命令.虽然它们实际上不是C语言的一部分,但却扩展了C程序设计的环境.本节将介绍如何应用预处理程序和注释简化程序开发过程,并提高程序的可读性.ANS ...

  9. (C#基础) byte[] 之初始化, 赋值,转换。

    byte[] 之初始化赋值 用for loop 赋值当然是最基本的方法,不过在C#里面还有其他的便捷方法. 1. 创建一个长度为10的byte数组,并且其中每个byte的值为0. byte[] myB ...

  10. 定时任务处理-Quartz

    Quartz Scheduler,定时任务 Quartz是一个作业调度系统(a job scheduling system),负责在约定的时间到达时执行(或通知)其他软件控制.是一个Java的定时任务 ...