插话:只写了几个连续的博客,博客排名不再是实际“远在千里之外”该。我们已经进入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 解决报告的更多相关文章

  1. Leetcode: LRU Cache 解题报告

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

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

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

  3. 【LeetCode】146. LRU Cache 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...

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

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

  5. LeetCode:LRU Cache

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

  6. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

  7. LeetCode: LRU Cache [146]

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

  8. LeetCode – LRU Cache (Java)

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

  9. [LeetCode] LRU Cache [Forward]

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

随机推荐

  1. PWA之Web 应用清单

    原文 简书原文:https://www.jianshu.com/p/5c96242188e8 大纲 1.什么是Web 应用清单 2.“清单文件”:Web App Manifest 规范的应用 3.we ...

  2. [CSS] Manipulate Images Using CSS Filter and Blend Modes

    Apply filters like blur, brightness, saturation and hue to images. Combined with CSS blend modes, yo ...

  3. 【转载】 Searching过程粗略梳理 分类: H4_SOLR/LUCENCE 2014-07-25 22:59 316人阅读 评论(0) 收藏

    转载自:http://www.cnblogs.com/huangfox/archive/2012/02/09/2344686.html solr-searching过程分析(一) --searchin ...

  4. 试用 Tomcat7.x 与 Tomcat6.x 的明显不同 + Context 填写方法 + 默认应用配置方法 (zhuan)

    http://blog.csdn.net/shanelooli/article/details/7408675

  5. kvm 虚拟化概述及 virt-manager 安装虚拟机

    一.KVM定义 基于内核的虚拟机(英语:Kernel-based Virtual Machine,简称KVM),是一种用于Linux内核中的虚拟化基础设施. KVM眼下支持Intel VT及AMD-V ...

  6. HDU4876:ZCC loves cards

    Problem Description ZCC loves playing cards. He has n magical cards and each has a number on it. He ...

  7. ajax跳转到新的jsp页面(局部刷新)

    ajax可以实现局部刷新页面,即在不刷新整个页面的情况下更新页面的局部信息. 项目中遇到一个问题:在用户列表也,当点击某个按钮时需要去查询用户的信息,查询成功跳转到用户详情界面:查询失败,则在原页面弹 ...

  8. IT忍者神龟之Hibernat持久化对象-数据表映射配置回想

    1.持久化对象POJO编写规则: 1) 有空參public构造器: 2) 提供标识属性.映射数据表主键: 3) 属性提供setter和getter方法. 4) 属性使用基本数据类型的包装类型.基本类型 ...

  9. C++省略号类型和参数个数不确定函数参数范例

    声明:所有权利保留. 转载必须说明出处:http://blog.csdn.net/cartzhang/article/details/44203651 今天想写个宏定义,发现宏定义里也可以写不定参数, ...

  10. Android加载图片导致内存溢出(Out of Memory异常)

    Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory  Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证) ...