【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际“远在千里之外”该。我们已经进入2一万内。
再接再厉。油!
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.
【题意】
设计并实现一个支持get和set操作的缓存:
get(key) - 存在时返回其值。否则返回-1。
set(key) - 不存在时插入新值。存在时更新其值。注意当容量满时,需删除最长时间没有訪问的key。将其删除。并插入新的key。
==================== Map+List 实现法 ====================
【思路】
用map结构实现<key, value>的存储与读取。
用一个list来记录key被訪问时间的久远。近期被訪问的放在list的最后。list中的第一个key表示最长时间没被訪问的。
【Java代码】
class LRUCache {
HashMap<Integer, Integer> map;
ArrayList<Integer> list;
int capacity;
public LRUCache(int capacity) {
map = new HashMap<Integer, Integer>(capacity);
list = new ArrayList<Integer>(capacity);
this.capacity = capacity;
}
public int get(int key) {
if (map.get(key) == null) return -1;
list.remove(new Integer(key));
list.add(key);
return map.get(key);
}
public void set(int key, int value) {
if (map.get(key) != null) {//原来存在key
map.put(key, value);
list.remove(new Integer(key));
list.add(key);
} else {//原来不存在key
if (map.size() < capacity) {//容量不满
map.put(key, value);
list.add(key);
} else {//容量满
int leastkey = list.remove(0);
list.add(key);
map.remove(leastkey);
map.put(key, value);
}
}
}
}
【注意点】
题目要求是Least Recently Used,不仅 set 时要更新list,get 时也要更新list。
set 时。需先推断map中有无该值,若没有再推断map是否满了;假设反过来,即先推断map是否为满,再推断map中有无该值。这样就错了。
由于假设map满时,当中有该值。直接更新就好,而先推断map是否为满的话。就会导致删除最长时间没有被訪问的值。
【常规解法】
通经常使用的元素双向链表来记录不被访问的时间最长,由于双向链表可以O(1)达到一定时间内移动的节点,删除头和尾节点。
在上面的代码list实现,其remove当实际遍历整个list为了找到一个节点。
LeetCode没有时间作要求,采访中肯定会要求。
【LeetCode】LRU Cache 解决报告的更多相关文章
- Leetcode: LRU Cache 解题报告
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 【LeetCode】146. LRU Cache 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LeetCode——LRU Cache
Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
- 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 [Forward]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- windows 下安装git
Git是当今最流行的版本控制软件,它包含了许多高级工具,这里小编就讲一下Git的安装. 首先如下图:(点击next) 第二步:文件位置存储,可根据自己盘的情况安装 第三步:安装配置文件,自己需要的都选 ...
- Java中String推断相等equals与==的差别以及StringBuilder的equals
Java中String类型具有一个equals的方法能够用于推断两种字符串是否相等,可是这样的相等又与运算符==所推断的"相等"有所不同,接下来进行分析,结论由程序进行验证 Str ...
- Java中sleep()与wait()区别
学习时正好碰到这两个方法,就查阅相关资料,并通过程序实现,进行区别一下: 1.每个对象都有一个锁来控制同步访问,Synchronized关键字可以和对象的锁交互,来实现同步方法或同步块.sleep() ...
- Apache DataFu: LinkedIn开源的Pig UDF库
介绍 Apache DataFu分两部分,本文介绍的是其Pig UDF的部分.代码在Github上开源(除了代码外.也有一些slides介绍链接). DataFu里面是一些Pig的UDF.主要包含这些 ...
- dom对象常用的属性和方法有哪些?
dom对象常用的属性和方法有哪些? 一.总结 一句话总结: 1.document属性和方法:document的属性有head,body之类,方法有各种获取element的方法 2.element的属性 ...
- 循环不变式(loop invariant)
循环不变式是一种条件式(必须满足的条件,对循环而言是保持不变的,无论循环执行了多少次),循环语句没执行一次,就要求中间的结果必须符合不变式的要求. (1)进入循环语句时,不变式必须成立: (2)循环语 ...
- UIlabel文字大小自适应label宽度变大变小
label.adjustsFontSizeToFitWidth = YES; //默认no
- 【BZOJ 1019】 [SHOI2008]汉诺塔
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1019 [题意] [题解] 这个题解讲得很清楚了 http://blog.sina.co ...
- 常见的面试C#技术题目
遍历查询窗体界面的textbox为空值 ? foreach (System.Windows.Forms.Control control in this.Controls) { ...
- Android在网络上分析获取图片(支持bmp格式)
Android学习系列 - 在网络上显示的图像(支持bmp格公式)) 参见例如,下面的代码: /** * 至Url地址上去照片.并返回Bitmap回来 * * @param i ...