【题目】

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.

【题意】

实现LRU策略

1. 依据key取value

2. 插入key-value时,须要删除LRU的item

【思路】

维护一个Map记录相应的<key, value>对

        为了模拟key的訪问先后关系,须要维护一个訪问次序列表,越靠后的节点,訪问时间距当前时间越短

而在insert或者訪问key的时候,须要从列表中找到相应的key,并把它调整到列表为。

这里遇到两个问题,一个是查找,还有一个是移动到末尾



假设使用顺序表,查找O(n),移动O(n),在cache规模非常大时时间代价过高

因此这里使用双向链表来处理

【代码】

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{
private:
Node* head;
Node* tail;
int capacity;
map<int, Node*>cache;
public:
LRUCache(int capacity) {
this->head = NULL;
this->tail = NULL;
this->capacity = capacity;
} void move2tail(Node* node){
if(node==tail)return;
if(node==head){
head = node->next;
head->prev=NULL;
tail->next=node;
node->prev=tail;
tail=node;
tail->next=NULL;
}
else{
node->prev->next = node->next;
node->next->prev = node->prev;
tail->next=node;
node->prev=tail;
tail=node;
tail->next=NULL;
}
} int get(int key) {
if(this->cache.find(key)==this->cache.end())return -1;
move2tail(this->cache[key]);
return this->cache[key]->val;
} void set(int key, int value) {
if(this->cache.find(key)==this->cache.end()){//cache中还没有
if(this->capacity==0){//cache已经满了
//删除头结点
this->cache.erase(head->key);
head=head->next;
if(head)head->prev=NULL;
else tail=NULL;
}
else{//cache还没满
this->capacity--;
}
//加入新节点
Node* newNode=new Node(key, value);
this->cache[key]=newNode;
if(tail){
tail->next=newNode;
newNode->prev=tail;
tail=newNode;
tail->next=NULL;
}
else{
head=tail=newNode;
}
}
else{//cache中已经有了
this->cache[key]->val = value;
move2tail(this->cache[key]);
}
}
};

LeetCode: LRU Cache [146]的更多相关文章

  1. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  2. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  3. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  4. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

  5. LeetCode – LRU Cache (Java)

    Problem Design and implement a data structure for Least Recently Used (LRU) cache. It should support ...

  6. Leetcode: LRU Cache 解题报告

    LRU Cache  Design and implement a data structure for Least Recently Used (LRU) cache. It should supp ...

  7. [LeetCode] LRU Cache [Forward]

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  8. Leetcode:LRU Cache,LFU Cache

    在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...

  9. 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 ...

随机推荐

  1. Tomcat免安装版的环境变量配置以及Eclipse下的Tomcat配置和测试

    Tomcat是目前比较流行的开源且免费的Web应用服务器,在我的电脑上第一次安装Tomcat,再经过网上教程和自己的摸索后,将这个过程 重新记录下来,以便以后如果忘记了可以随时查看. 注意:首先要明确 ...

  2. light oj 1047-neighbor house

    ime Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Description The people ...

  3. [Swust OJ 137]--波浪数(hash+波浪数构造)

    题目链接:http://acm.swust.edu.cn/problem/137/ Time limit(ms): 1000 Memory limit(kb): 65535   Description ...

  4. Python 2.7 学习笔记 基本语法和函数定义

    本文介绍下python的基本语法 一.变量定义 不需要说明类型,也不需要像js等脚本语言使用var等标识符.直接声明即可,如: num=1 说明:上面语句声明了一个变量num,并在声明时初始化值为 1 ...

  5. mybatis-redis项目分析

    redis作为现在最优秀的key-value数据库,非常适合提供项目的缓存服务.把redis作为mybatis的查询缓存也是很常见的做法.在网上发现N多人是自己做的Cache,其实在mybatis的g ...

  6. java之Thread.sleep(long)与object.wait()/object.wait(long)的区别(转)

    一.Thread.sleep(long)与object.wait()/object.wait(long)的区别sleep(long)与wait()/wait(long)行为上有些类似,主要区别如下:1 ...

  7. python IDE ulipad配置使用

    一直认为认为python自带的编辑器idle实在是太简陋了,连显示行号的功能都没有,也非常不好编辑.找了下windows平台下的Ide,发现ulipad很多人推荐使用,就开始安装了. 首先去官网: h ...

  8. Codeforces Round #312 (Div. 2)

    好吧,再一次被水题虐了. A. Lala Land and Apple Trees 敲码小技巧:故意添加两个苹果树(-1000000000, 0)和(1000000000, 0)(前者是位置,后者是价 ...

  9. THUSC2015

    这些题目在BZOJ上面有,可惜是权限题.话说BZOJ上面的题目真的挺好的,要不是我穷,早就去弄个号了! 言归正传,今年的题目难度可以由一个名人名言看出: 题目太水.--某某神犇 可是我掂量了一下,发现 ...

  10. 求知成瘾+逻辑成瘾+博识的无知,你中枪没?我感觉中枪了 - 外野 - Stage1st - Powered by Discuz!

    求知成瘾+逻辑成瘾+博识的无知,你中枪没?我感觉中枪了 - 外野 - Stage1st - Powered by Discuz! 求知成瘾 求知欲似乎是人们的本能,尤其「好学」这个词被定义成天生的褒义 ...