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. 安卓开发, 遇到WebView不能加载静态网页, WebView显示 "net::ERR_PROXY_CONNECTON_FAILED"

    http://blog.csdn.net/zhouchangshi/article/details/44454695 Android开发中遇到网络连接问题, 要找WebView中显示一个静态的网页, ...

  2. vector的 emplace 和 insert 以及使用vector进行iterator遍历 且 erase的时候注意事项

    vector<int> first;//Size()==2 first.push_back(); first.push_back(); //first.insert(2); vector& ...

  3. 使用eclipse JDT compile class,解决 无法确定 X 的类型参数;对于上限为 X,java.lang.Object 的类型变量 X,不存在唯一最大实例

    ant 命令行方式执行build javac编译class出现 泛型无法转换 无法确定 <X>X 的类型参数:对于上限为 X,java.lang.Object 的类型变量 X,不存在唯一最 ...

  4. GBK、UTF8、UNICODE编码转换

    string GBKToUTF8(const std::string& strGBK) { , strGBK.c_str(), -, NULL, ); WCHAR * wszUTF8 = ne ...

  5. openCV_java Canny边缘检测

    边缘检测的原理: 检测出图像中所有灰度值变化较大的点,而且这些点连起来构成若干线条,这些线条就称之为图像的边缘. 1986年,由John F. Canny 提出! // Canny(Mat image ...

  6. python查找算法的实现-二分法

    1.算法:(设查找的数组期间为array[low, high]) (1)确定该期间的中间位置K(2)将查找的值T与array[k]比较.若相等,查找成功返回此位置:否则确定新的查找区域,继续二分查找. ...

  7. 网页内容导出word/excel的js代码

    IE设置: 工具-> Internet选项-> 安全->自定义级别-> 对没有标记安全级别的ActiveX控件进行初始化  设为启用! 1.导出word //指定区域导出到Wo ...

  8. CSV - 操作比较

    在数据报表生成的时候,我们一般会用程序去生成CSV.其中有些需要注意的地方. log_file = open('delay.%s.csv' % s_end, 'w') log_file.write(' ...

  9. 使用 Python SimpleHTTPServer 快速共享文件

    近期,想着从一个服务器 向另一个服务器传输文件,但是对其知之甚少,就从别人那里知道一种方法,使用 Python SimpleHTTPServer 快速共享文件. 直接运行:python -m Simp ...

  10. WPF笔记一

    笔记内容: BUG.WPF运行窗体时调用Hide()方法,然后再Show()异常的解决方案 WPF 窗体设置为无边框 选择本地文件 选择文件夹 WPF实现右下角弹出消息窗口 WPF 显示 HTTP 网 ...