【题目】

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. centos安装vim7.4

    转载于:http://www.cnblogs.com/nhlinkin/p/3545509.html    系统版本centos6.4; root权限 su - root     卸载 $ rpm - ...

  2. vs2013内置IISExpress相关问题

    问题描述,以前做的程序迁移到vs2013后出现500.22问题. HTTP 错误 500.22 - Internal Server Error 检测到在集成的托管管道模式下不适用的 ASP.NET 设 ...

  3. 使用Intel编译器获得一致的浮点数值计算结果

    使用Intel编译器获得一致的浮点数值计算结果大多数十进制的浮点数, 用二进制表示时不是完全一致的; 与此同时, 大多数与浮点数值相关的计算结果, 存在着固有的不确定性.通常, 编写浮点计算应用软件希 ...

  4. windows 下搭建 apache + php52 + postgreSQL7/8/9环境

    apache和php安装参考:[转]Windows7 64bit下配置Apache+PHP+MySQL 我这主要讲配置  apache 支持 postgresql9数据库: 1.将php5文件夹下的p ...

  5. Python之路Day10

    本节主要内容:memcache&redis.RabbitMQ.twisted框架 1. memcache&redis 1.1 memcache Memcached 是一个高性能的分布式 ...

  6. java --- 对象的创建过程

    java 对象创建的过程 存在了继承关系之后,对象创建过程如下: 1.分配空间.要注意的是,分配空间不光是分配子类的空间,子类对象中包含的父类对象所需要的空间,一样在这一步统一分配.在分配的空间的时候 ...

  7. docker学习笔记17:Dockerfile 指令 ONBUILD介绍

    ONBUILD指令可以为镜像添加触发器.其参数是任意一个Dockerfile 指令. 当我们在一个Dockerfile文件中加上ONBUILD指令,该指令对利用该Dockerfile构建镜像(比如为A ...

  8. c++ namespace命名空间详解

    What is a namespace? A namespace defines an area of code in which all identifiers are guaranteed to ...

  9. POJ 2777 线段树基础题

    题意: 给你一个长度为N的线段数,一开始每个树的颜色都是1,然后有2个操作. 第一个操作,将区间[a , b ]的颜色换成c. 第二个操作,输出区间[a , b ]不同颜色的总数. 直接线段树搞之.不 ...

  10. MySQL - 主从复制及读写分离原理

    1.MySQL主从复制与读写分离原理 首先,我们看一个图: 影响MySQL-A数据库的操作,在数据库执行后,都会写入本地的日志系统A中. 假设,实时的将变化了的日志系统中的数据库事件操作,在MYSQL ...