LeetCode——LRU Cache
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的更多相关文章
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LeetCode: LRU Cache [146]
[题目] Design and implement a data structure for Least Recently Used (LRU) cache. It should support th ...
- LeetCode – LRU Cache (Java)
Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support ...
- Leetcode: LRU Cache 解题报告
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...
- [LeetCode] LRU Cache [Forward]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Leetcode:LRU Cache,LFU Cache
在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...
- 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 ...
随机推荐
- Multiplexing SDIO Devices Using MAX II or CoolRunner-II CPLD
XAPP906 Supporting Multiple SD Devices with CoolRunner-II CPLDs There has been an increasing demand ...
- 解决连接MySql速度慢的方法
最近在Linux服务器上安装MySql5后,本地使用客户端连MySql速度超慢,本地程序连接也超慢.解决方法:在配置文件my.cnf的[mysqld]下加入skip-name-resolve. 原因是 ...
- 那些在学习iOS开发前就应该知道的事(part 1)
英文原文:Things I wish I had known before starting iOS development—Part 1 http://www.cocoachina.com/ios/ ...
- 让淘宝链接在微信中分享,GO
老婆开了个淘宝店,小卖家的心理大家都知道,一开始的小店总是没有光顾,吸引流量成了重中之中. 所以她到处发链接,微信里发了好多条,可是没过多久有好友微她,问她发的时什么,为什么都是打不开的.结果直接傻眼 ...
- apache 80端口部属多站点配置
1.在httpd.conf文件里启用虚拟主机功能,即去掉下面配置项前面的# #LoadModule vhost_alias_module modules/mod_vhost_alias.so 2..在 ...
- [转] Visual Studio Code behind a proxy
http://www.tuicool.com/articles/jyyIBf3 http://blog.majcica.com/2016/04/07/visual-studio-code-behind ...
- Android学习笔记----Activity的生命周期图示
转载,一目了然.
- 手动安装 atom 扩展包 packages
由于某些原因, 我们下载 atom 扩展时发现速度特别慢, 或者根本无法下载, 那我们可以尝试手动安装 首先, 从 github 上下载(或其它地方) 扩展包, 解压 进入该文件夹, 找到 packa ...
- Android手机的 storage
老外的一段解释 -------------------------------------------------------------------------------------------- ...
- TSPL学习笔记(3):排序算法练习
快速排序 快排的详细介绍见,简单的说就是取输入序列中的首元素m,然后将除首元素m以外的其它元素分成两组,小于等于m的一组和大于m的一组.将3组元素组合成输入队列:小于等于m + m + 大于m. 下面 ...