插话:只写了几个连续的博客,博客排名不再是实际“远在千里之外”该。我们已经进入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. java根据xml配置文件导出excel通用方法

    java web项目中时常会用到导出功能,而导出excel几乎是每个项目必备的功能之一.针对形形色色的导出方法及个人平时的工作经验,特将导出excel方法整理成通用的方法,根据xml配置来实现特定的导 ...

  2. 在线算法与离线算法(online or offline)

    1. 在线算法(online) PFC(prefix-free code)编码树的解码过程:可以在二进制编码串的接收过程中实时进行,而不必等到所有比特位都到达后才开始: 2. 离线算法(offline ...

  3. windows安装rabbitMQ服务

    简介: RabbitMQ是流行的开源消息队列系统,用erlang语言开发.RabbitMQ是AMQP(高级消息队列协议)的标准实现. windows安装rabbitMQ服务步骤: 首先需要安装 Erl ...

  4. Java环境搭建若干问题

    0.总体说明   本次搭建环境,为了偷懒,使用的是,阿里云镜像.   自带了Nginx.Tomcat.JDK等.   比较坑爹的是,虽然镜像带了很多安装好的软件,但是也有各种问题,比如它修改了tomc ...

  5. html+css+js实现狼吃羊小游戏

    html+css+js实现狼吃羊小游戏 一.总结 一句话总结:给动的元素下标记,这里表现为将要活动的标签动态增加class,这是一种很好的思想. 1.如何实现棋子走动的时候简单精确定位? 用重构坐标系 ...

  6. iOS中html5的交互:WebViewJavascriptBridge

    https://github.com/marcuswestin/WebViewJavascriptBridge Setup & Examples (iOS & OSX) Start w ...

  7. Python应用库大全总结

    学python,想必大家都是从爬虫开始的吧.毕竟网上类似的资源很丰富,开源项目也非常多. python学习网络爬虫主要分3个大的版块:抓取,分析,存储 当我们在浏览器中输入一个url后回车,后台会发生 ...

  8. 高性能分布式应用开发中间件ICE介绍

    作为一个技术人员,你是否在为不断增长的数据量和日益复杂的业务逻辑而头疼不已,杂乱堆砌在一起的庞大业务让系统越来越脆弱,于是你想到了网格,想到了利用分布式来重组一个健壮的系统架构. 随后,RMI,EJB ...

  9. bootstrap paginator使用简述

    1.引入bootstrap-paginator.js(依赖jquery)2.html部分需要一个ul标签,接下来我们会把该标签设置为分页组件 3.js部分(核心) 注意事项: 1.通过onPageCl ...

  10. 基于go语言的心跳响应

    我们在使用tcp ip 通讯的时候,都需要使用心跳机制来判断服务器与客户端的连接状态,如果服务器的心跳超时等,会做出重新连接等机制, 基于这种问题,我今天给大家推荐了一个基于go语言的心跳响应机制,废 ...