[LeetCode] LRU Cache [Forward]
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.
struct node {
node* pre;
int key;
int value;
node* next;
node(int k, int v):key(k),value(v),pre(NULL),next(NULL) {};
}; class LRUCache {
map<int, node*> mp;
node* head;
node* tail;
int size;
int capacity;
public:
LRUCache(int c) {
if (c < )return;
head = new node(, );
tail = new node(, );
head->next = tail;
tail->pre = head;
mp.clear();
size = ;
capacity = c;
} int get(int k) {
map<int, node*>::iterator it = mp.find(k);
if (it != mp.end()) {
node* cur = (*it).second;
cur->pre->next = cur->next;
cur->next->pre = cur->pre;
putToHead(cur);
return cur->value;
} else
return -;
} void set(int k, int val) {
if (capacity < )return;
map<int, node*>::iterator it = mp.find(k);
if (it != mp.end()) {//find
node* cur = (*it).second;
cur->pre->next = cur->next;
cur->next->pre = cur->pre;
cur->value = val;
putToHead(cur);
} else {//not find
node* tmp = new node(k,val);
putToHead(tmp);
mp[k] = tmp;
if (size < capacity) {//size < capacity
size++;
} else {//size >= capacity
node* deltmp = tail->pre;
tail->pre = deltmp->pre;
deltmp->pre->next = tail;
it = mp.find(deltmp->key);
mp.erase(it);
delete deltmp;
}
}
}
void putToHead(node* cur)
{
cur->next = head->next;
cur->pre = head;
cur->next->pre = cur;
head->next = cur;
} };
[LeetCode] LRU Cache [Forward]的更多相关文章
- [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
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- 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,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 ...
随机推荐
- 每日命令:(12)sar
sar(System Activity Reporter系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情况.系统调用的使用 ...
- 安装Vmware Tools出现错误
安装Vmware Tools出现: Before you can compile modules, you need to have the following installed... makegc ...
- 树莓派 -- 输入设备驱动 (key)
输入设备(如按键,键盘,触摸屏等)是典型的字符设备,其一般工作原理是底层在按键或触摸等动作发生时产生一个中断,然后CPU通过SPI,I2C总线读取键值. 在这些工作中之后中断和读键值是与设备相关的,而 ...
- ruby cloud9部署到heroku
Cloud9网址:https://c9.io/ 使用github账号登陆,如果没有,现在github(https://github.com/)上注册一个用户,在进行登陆.
- Caused by: java.io.IOException: 您的主机中的软件中止了一个已建立的连接。
异常详情 2017-07-16 10:55:26,218 ERROR [500.jsp] - java.io.IOException: 你的主机中的软件中止了一个已建立的连接. org.apache. ...
- 集训第六周 数学概念与方法 J题 数论,质因数分解
Description Tomorrow is contest day, Are you all ready? We have been training for 45 days, and all g ...
- oracle spool
http://peter8015.iteye.com/blog/2082467 关于SPOOL(SPOOL是SQLPLUS的命令,不是SQL语法里面的东西.) 对于SPOOL数据的SQL,最好要自己定 ...
- hdu 1698区间延迟更新
#include<stdio.h> #define N 100100 struct node { int x,y,yanchi; }a[N*4];//注意数组范围 void build(i ...
- android view自定义
转载自:http://blog.csdn.net/iispring/article/details/50708044
- 深入理解hadoop(二)
hadoop RPC 网络通信是hadoop的核心模块之一,他支撑了整个Hadoop的上层分布式应用(HBASE.HDFS.MapReduce), Hadoop RPC具有以下几个特性,透明性(用户本 ...