LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构。 题目链接。关于LRU的基本知识可参考here
分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::list)和哈希表(std::unordered_map)作为cache的数据结构,因为:
- 双向链表插入删除效率高(单向链表插入和删除时,还要查找节点的前节点)
- 哈希表保存每个节点的地址,可以基本保证在O(1)时间内查找节点
具体实现细节:
- 越靠近链表头部,表示节点上次访问距离现在时间最短,尾部的节点表示最近访问最少
- 查询或者访问节点时,如果节点存在,把该节点交换到链表头部,同时更新hash表中该节点的地址
- 插入节点时,如果cache的size达到了上限,则删除尾部节点,同时要在hash表中删除对应的项。新节点都插入链表头部。 本文地址
代码如下:
struct CacheNode
{
int key;
int value;
CacheNode(int k, int v):key(k), value(v){}
}; class LRUCache{
public:
LRUCache(int capacity) {
size = capacity;
} int get(int key) {
if(cacheMap.find(key) == cacheMap.end())
return -;
else
{
//把当前访问的节点移到链表头部,并且更新map中该节点的地址
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
cacheMap[key] = cacheList.begin();
return cacheMap[key]->value;
} } void set(int key, int value) {
if(cacheMap.find(key) == cacheMap.end())
{
if(cacheList.size() == size)
{//删除链表尾部节点(最少访问的节点)
cacheMap.erase(cacheList.back().key);
cacheList.pop_back();
}
//插入新节点到链表头部,并且更新map中增加该节点
cacheList.push_front(CacheNode(key, value));
cacheMap[key] = cacheList.begin();
}
else
{//更新节点的值,把当前访问的节点移到链表头部,并且更新map中该节点的地址
cacheMap[key]->value = value;
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
cacheMap[key] = cacheList.begin();
} }
private:
list<CacheNode> cacheList;
unordered_map<int, list<CacheNode>::iterator>cacheMap;
int size;
};
【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3417157.html
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
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 解题报告
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 ...
随机推荐
- javascript 模式(1)——代码复用
程序的开发离不开代码的复用,通过代码复用可以减少开发和维护成本,在谈及代码复用的时候,会首先想到继承性,但继承并不是解决代码复用的唯一方式,还有其他的复用模式比如对象组合.本节将会讲解多种继承模式以实 ...
- 移动端UC /QQ 浏览器的部分私有Meta 属性
<meta name="format-detection" content ="telephone=no"/> 格式检测 禁止识别我们页面中的数 ...
- JQ中的方法、事件及动画
css( ) 除了可以为元素添加样式外,还可用来查询元素,某样式值alert($('.cls1').css('width')); //100px(返回带单位的值)注意:原生CSS样式中有-的去掉并且将 ...
- 基础算法(javascipt)总结
一.排序: 1.选择排序: 2.交换排序: 3.插入排序 二.查找: 三.节点遍历: 四.数组去重: 时间复杂度:找出算法中的基本语句->计算基本语句的执行次数的数量级->用大O记号表示算 ...
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(八)资源搜索模块
config.xml文件的配置如下: <widget label="资源搜索" icon="assets/images/public_impact_over.png ...
- Mac ping localhost 地址变化
title: Mac ping localhost 地址变化date: 2016-1-15 16:21:55categories: IOS tags: mac 小小程序猿我的博客:http://day ...
- UIToolBar
//UIToolBar 是导航控制器默认隐藏的工具条 //设置UIToolBar的隐藏状态 self.navigationController.toolbarHidden = NO; //如何找到UI ...
- Android编码规范04
private final String MESSAGE_WARN = "您输入的密码有误,请重新输入!"; private final String CLASS_ONE = &q ...
- 记CentOS-7-x86_64-DVD-1503与Windows7单硬盘双系统的安装
我最初的设想是:Win引导CentOS,最后却变成了CentOS引导Win了.算是‘弄拙成巧’了吧. 因为我打算用U盘刻录镜像直接从U盘启动,所以不需要网上一些教程里面的繁琐的win下引导CentOS ...
- 解析UML箭头、线条代表的意义(转)
在学习UML过程中,你经常会遇到UML类图关系,这里就向大家介绍一下UML箭头.线条代表的意义,相信通过本文的介绍你对UML中箭头.线条的意义有更明确的认识. AD: 本节向大家学习一下UML箭头.线 ...