【LeetCode】146. LRU Cache
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的更多相关文章
- 【LeetCode】146. LRU Cache 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- LeetCode题解: LRU Cache 缓存设计
LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode 版权声明:本文为博主原创文章,遵循CC 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 ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
随机推荐
- leetcode笔记:Same Tree
一. 题目描写叙述 Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- MySQL中的模糊查询和通配符转义
MySQL中实现模糊查询有2种方式:一是用LIKE/NOT LIKE,二是用REGEXP/NOT REGEXP(或RLIKE/NOT RLIKE,它们是同义词). 第一种是标准的SQL模式匹配.它有2 ...
- 什么是C++虚函数、虚函数的作用和使用方法
我们知道,在同一类中是不能定义两个名字相同.参数个数和类型都相同的函数的,否则就是“重复定义”.但是在类的继承层次结构中,在不同的层次中可以出现名字相同.参数个数和类型都相同而功能不同的函数.例如在例 ...
- 附 5 springboot之配置文件
本文转载自http://www.jianshu.com/p/80621291373b,作者:龙白一梦 我的boss 代码从开发到测试要经过各种环境,开发环境,测试环境,demo环境,线上环境,各种环境 ...
- Android组件之Service浅谈
Service是Android中的四大组件之一,和windows中的服务是类似,服务一般没有用户操作界面,它运行于系统中不容易被用户发觉,可以使用它开发如监控之类的程序Service,手机中有的程序的 ...
- document.execCommand("BackgroundImageCache",false,true)解决ie6下的背景图片缓存问题
E6下的背景图片每次使用都会重新发送请求(not 本地),连一个hover效果时候同样的背景图片仅仅位置不同而已,ie6都会再次发送请求,这个令人崩溃的事情需要解决掉:对于ie来说,filter:ex ...
- 如何解决 SQL Server 中的锁升级所致的阻塞问题
概要 锁升级为表锁插入转换很多细粒度的锁 (如行或页锁) 的过程.Microsoft SQL Server 动态确定何时执行锁升级.作出决定之前,SQL Server 将特定的扫描,整个事务,并且用于 ...
- 理解JavaScript模仿块作用域
1.JS没有块作用域 在C和Java中,一对大括号{}决定一个作用域,比如for循环.在js中,变量可以在函数任何一处定义,并且忽略重复定义.变量初始化之前使用,值永远是undefined. func ...
- sqoop安装部署(笔记)
sqoop是一个把关系型数据库数据抽向hadoop的工具.同时,也支持将hive.pig等查询的结果导入关系型数据库中存储.由于,笔者部署的hadoop版本是2.2.0,所以sqoop的版本是:sqo ...
- PHP高级教程-Cookie
PHP Cookie cookie 常用于识别用户. Cookie 是什么? cookie 常用于识别用户.cookie 是一种服务器留在用户计算机上的小文件.每当同一台计算机通过浏览器请求页面时,这 ...