LRU Cache

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.

这题关键在于,怎样判断每个value是否算“最近使用”?

一个简单的想法是对每个键值对保留一个年龄,当cache满时,删除最“老”的键值对。

然而在删除节点时,寻找最“老”节点需要O(n)时间。

因此建立一个双向链表,最近使用的调整到头部,需要删除则删除尾部。

这样寻找最“老”节点就为O(1)时间。

然而在get函数时查找所需节点仍为O(n)时间。

因此再加入映射表m,空间换时间,查找变为O(1)。

struct Node
{
int key;
int val;
Node* prev;
Node* next;
Node(int k, int v): key(k), val(v), prev(NULL), next(NULL) {}
}; class LRUCache{
public:
Node* head; //most recently used
Node* tail; //least recently used
unordered_map<int, Node*> m;
int curcap;
int maxcap; LRUCache(int capacity) {
head = new Node(-, -);
tail = new Node(-, -);
head->next = tail;
tail->prev = head;
curcap = ;
maxcap = capacity;
} int get(int key) {
if(m[key] == NULL)
return -;
else
{
Node* node = m[key];
delnode(node);
addtohead(node);
return node->val;
}
} void set(int key, int value) {
if(m[key] == NULL)
{
if(curcap == maxcap)
{
m[tail->prev->key] = NULL;
delnode(tail->prev);
}
Node* node = new Node(key, value);
addtohead(node);
m[key] = node;
if(curcap < maxcap)
curcap ++;
}
else
{
Node* node = m[key];
node->val = value;
delnode(node);
addtohead(node);
}
} void delnode(Node* node)
{
Node* prev = node->prev;
Node* next = node->next;
prev->next = next;
next->prev = prev;
} void addtohead(Node* node)
{
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
}
};

【LeetCode】146. LRU Cache的更多相关文章

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

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

  2. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  3. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. [Android Pro] Swift 3.0多线程

    本文只介绍Grand Central Dispath(GCD) 中央调度 个人认为一个GCD就够用了,可能是改版或是其他的在找之前写的多线程方法时发现不能用了,看文档之后发现改了,现在看上去更加简单易 ...

  2. 【BZOJ】【1014】【JLOI2008】火星人prefix

    Splay/二分/Hash 看了网上的题目关键字(都不用点进去看……我也是醉了)了解到做法= =那就上呗,前面做了好几道Splay的题就是为了练手搞这个的. Hash判断字符串是否相同应该很好理解吧? ...

  3. 以ScaleIO 1.30为后端存储运行微软服务器软件SQL Server 2014, SharePoint 2013, Exchange 2013的解决方案

    EMC新发布了以ScaleIO 1.30为后端存储来运行SQL, SharePoint, Exchange的解决方案白皮书.   下面的页面中有简要的介绍和整篇文档PDF的下载. https://co ...

  4. 删除在Godaddy注册的域名,申请退款的全过程

    1,删除域名. 登录进 Godaddy ,进入域名管理(Domain Manager),点击你要删除的域名,把要删除的域名前面打上对勾,再点击“delete selected”,确认,稍等一会就删除了 ...

  5. Spring Boot应用连接数据库MySQL、及一个简单的demo

    一.修改pom.xml文件 在项目的pom.xml文件上增加如下代码,添加依赖文件. <dependency> <groupId>mysql</groupId> & ...

  6. Ngxtop-Nginx日志实时分析利器

    ngxtop实时解析nginx访问日志,并且将处理结果输出到终端,功能类似于系统命令top,所以这个软件起名ngxtop.有了ngxtop,你可以实时了解到当前nginx的访问状况,再也不需要tail ...

  7. Package.json小结

        生成package.json 定位到想放置package.json的目录,运行npm init,根据提示就可以生成package.json文件,其中test command可以为空. 安装mo ...

  8. jquery文字填写自动高度

    下面开始写一个jquery插件 (function($){ $.fn.autoTextarea = function(options) { var defaults={ maxHeight:null, ...

  9. Pinger2

    import java.io.IOException;import java.io.InputStreamReader;import java.io.LineNumberReader;import j ...

  10. WebService SOAP、Restful和HTTP(post/get)请求区别

    web service(SOAP) Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力. Web service 就是一个应用程序,它向外界暴露出一个能够通过We ...