Description:

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.

题目大意是让设计一个基于LRU的缓存,即最近最后用的保留。

实现LRU缓存有几种方法,链表+HashMap结构实现,LinkedHashMap的实现(继承、组合)、FIFO实现。

具体见我的博客:

这里使用LikedHashMap的组合实现,简单高效。

 public class LRUCache {

     private int capacity;

     private java.util.LinkedHashMap<Integer, Integer> cache = new java.util.LinkedHashMap<Integer, Integer>(capacity,0.75f,true) {
@Override
protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
return size() > capacity;
}
}; public LRUCache(int capacity) {
this.capacity = capacity;
} public int get(int key) {
Integer res = cache.get(key);
return res==null?-1:res;
} public void set(int key, int value) {
cache.put(key, value);
}
}

网上比较好的答案代码也有上百行,时间也是几百毫秒,这样看起来JDK中的LinkedHashMap的实现还是很高效的。

在不必要的情况下最好不要重复造轮子——大神

LeetCode——LRU Cache的更多相关文章

  1. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

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

  2. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

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

  3. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  4. LeetCode: LRU Cache [146]

    [题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...

  5. LeetCode – LRU Cache (Java)

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

  6. Leetcode: LRU Cache 解题报告

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

  7. [LeetCode] LRU Cache [Forward]

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

  8. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

  9. leetcode LRU Cache python

    class Node(object): def __init__(self,k,x): self.key=k self.val=x self.prev=None self.next=None clas ...

随机推荐

  1. eclipse安装activiti工作流插件

    方式一:在有网络的情况下,安装流程设计器步骤如下: 1.点击eclipse上方工具栏的Help,选择Install New Software 2.弹出如下窗口,然后填写插件名称和安装地址 Name: ...

  2. NoteExpress 自定义参考文献输出样式

    最近在写硕士论文,样式中没有符合要求的,于是就自己来编辑下样式文件,下文是网上搜集的关于自定义参考文献的一篇教程,可以参考. 一.分隔符|和连接符*的使用 (1)连接符 * 主要作用是“建立依赖关系” ...

  3. windows10 qt5 mingw32编译cryptopp563

    windows10 qt5 mingw32编译cryptopp563 参考链接: http://www.qtcentre.org/threads/28809-Compiling-amp-using-C ...

  4. malloc钩子和内存泄漏工具mtrace、Valgrind

    一:malloc钩子函数 static void* (* old_malloc_hook) (size_t,const void *);static void (* old_free_hook)(vo ...

  5. java/.net-常用工具下载地址&常用学习网址&快捷键

    HTML5 HTML5:http://www.html5cn.org/ php常用网址 thinkphp框架:http://www.thinkphp.cn/ wampserver开发服务器:http: ...

  6. EventBus 3.0使用

    在没用eventBus之前一直用Android广播方式通知消息更新UI 广播写法 首先发送广播通知 Intent intent = new Intent(); intent.setAction(&qu ...

  7. js事件之event.preventDefault()与event.stopPropagation()用法区别

    event.preventDefault()用法介绍 该方法将通知 Web 浏览器不要执行与事件关联的默认动作(如果存在这样的动作).例如,如果 type 属性是 "submit" ...

  8. java项目中读取properties文件

    这里的配置文件都放在src下面, System.properties的内容 exceptionMapping=exceptionMapping.properties config=config.pro ...

  9. requirejs、backbone.js配置

    requirejs初探 参考资料官网:http://requirejs.org中文译文:http://makingmobile.org/docs/tools/requirejs-api-zh reuq ...

  10. 15.6.8-sql小技巧

    取月头月尾: declare @someDay datetime,@firstDay datetime,@endDay datetime set @someDay='2015.2.2' ,) ,) s ...