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 ...
随机推荐
- cocos2d-x 2.x版本接入bugly的总结
最开始项目使用的是自己DIY的很简陋的上报系统,后来改成google breakpad来上报,发现其实都做的不太理想,游戏引擎因为版本历史问题存在一些崩溃问题.后来3.x接入了bugly,我这边抽了几 ...
- 永久删除 tadb.exe
系统安装qq手机管家等手机管理软件之后,在开机时会自动运行tadb.exe, 这个垃圾进程会让开发android程序时默认的 adb.exe启动不了,这样就没法用手机调试. 这并不是说tadb.exe ...
- ThinkBox DOC
插件源码下载 @github https://github.com/Aoiujz/ThinkBox.git 插件使用方法 引入文件 //使用ThinkBox弹出框必须引入以上三个文件. //jQuer ...
- 收不到Win10正式版预订通知?一个批处理搞定
目前,已经有不少Win7.Win8.1用户在系统右下角收到Win10正式版的预订提示窗口.点击接受预订后,系统会将Win10正式版所需的安装文件提前下载好,7月29日正式发布的时候,就可以第一时间升级 ...
- javamail邮件发送例子
public class EmailTask{ // Session used by the javamail classes private Session session; ...
- THinkphp开启静态(动态)缓存的用法
<?php return array( //开启静态缓存 'HTML_CACHE_ON' => true, 'HTML_CACHE_RULES' => array( 'News:in ...
- Thinkpad X240使用U盘安装Win7系统
更改BIOS设置 不同电脑的进入BIOS的方式可能不太一样,Thinkpad X240的进入方式是在电脑启动的时候按下回车键,然后按F1进入BIOS. 1. 修改secure boot为Disable ...
- WPF 获取程序路径的一些方法,根据程序路径获取程序集信息
一.WPF 获取程序路径的一些方法方式一 应用程序域 //获取基目录即当前工作目录 string str_1 = System.AppDomain.CurrentDomain.BaseDirector ...
- [知识库分享系列] 四、ASP.NET MVC and Winform
知识库分享系列: [知识库分享系列] 三.Web(高性能Web站点建设) [知识库分享系列] 二..NET(ASP.NET) [知识库分享系列] 一.开篇 分享介绍本篇分享两个知识库节点,分别为“AS ...
- [GraphQL] Use GraphQL's List Type for Collections
In order to handle collections of items in a GraphQL Schema, GraphQL has a List Type. In this video, ...