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 ...
随机推荐
- Ext.NET MVC 配置问题总结
随着VS版本和.NET MVC版本.EF的版本的不断更新,虽然很多功能随着版本的提升而更完善,但对于旧版本开发的软件就有点悲催了,或许很多开发者都遇到类似的问题! 最近有一个项目是用.NET MVC3 ...
- TinyMCE 官方插件一览表(不完全)
TinyMCE 官方插件一览表:advlist(Advanced List Plugin):项目编号.toolbar:bullist.autolink:自动加链接.lists:This list pl ...
- redhat6.4安装MySQL-server-5.5.28-1.linux2.6.x86_64.rpm
首先下载下面三个文件: MySQL-server-5.5.28-1.linux2.6.x86_64.rpm MySQL-client-5.5.28-1.linux2.6.x86_64.rpm MySQ ...
- table tr foreach td 换行
@{ ;} <table style=" class="sy_table"> <tr> @foreach (DataRow dr in (View ...
- 基于WebGL的三维地形渲染
1.生成WebMap页面 #!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess from jinja2 import Envi ...
- SharePoint 2013 定制搜索显示模板
前言 之前我们已经介绍了一些关于搜索的相关配置,当然,用户关于搜索的要求可能是各种各样.有时候,用户会说,你们的显示结果太Low了,确实是:不过,在SharePoint中,我们可以很容易的定制搜索结果 ...
- Oracle system identifier(SID) "xxx" alread exits. Specify another SID
案例环境: 操作系统 :Oracle Linux Server release 5.7 64 bit 数据库版本:Oracle Database 10g Release 10.2.0.4.0 - ...
- ORACLE DELETE数据慢的案例
今天遇到一个有意思的案例,一开发同事告诉我他删除一个表的记录非常慢,已经快1个多小时了还没有完成.而且删除的记录只有1百多条.真是大跌眼镜的一件事情.最后发现该表与多个表有外键关联关系(这个表即是主表 ...
- sp_addlinkedserver '(null)' is an invalid product name
使用SSMS 2008客户端工具逆向生成了创建链接服务器的脚本时,在测试环境执行是报如下错误:'(null)' is an invalid product name. USE [master] GO ...
- eclipse svn分支与合并操作
以前做项目的时候没有用过svn的分支合并操作,今天用到了,刚开始还真不会啊.最后查了下就是这么的方便.专门记录下来. 原文来自:http://blog.csdn.net/lisq037/article ...