【题目】

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. Java线程(十):CAS

    前言 在Java并发包中有这样一个包,java.util.concurrent.atomic,该包是对Java部分数据类型的原子封装,在原有数据类型的基础上,提供了原子性的操作方法,保证了线程安全.以 ...

  2. ThinkPHP - 扩展个人类库 - 以验证码类为例子

    首先,在项目目录下创建Class文件夹,用于存储个人类文件. 之后建立Data目录存放所需字体文件,其他的数据也可以放在这个文件夹下. 然后再Conf文件夹下创建verify.php配置文件. 在co ...

  3. 【IE】浏览器模式与文档模式 及其开发中处理方式

    原文:http://blog.csdn.net/neo_liu0000/article/details/7589731 什么是浏览器模式和文本模式? 经常使用IE开发者工具的同学,肯定见过浏览器模式和 ...

  4. 【Oracle】number类型保留小数位

    SQL> SELECT TO_CHAR(, '9990.00') A, TO_CHAR(5.8, '9990.00') B, TO_CHAR(., '9990.00') C FROM dual; ...

  5. Python之路 Day12

    day12主要内容:html基础.CSS基础 HTML HTML概述: HTML是英文Hyper Text Mark-up Language(超文本标记语言)的缩写,他是一种制作万维网页面标准语言(标 ...

  6. POJ 1458 最长公共子序列 LCS

    经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1 ...

  7. docker学习笔记14:Dockerfile 指令 ENV介绍

    ENV指令用来在镜像构建过程中设置环境变量.我们来看一个Dockerfile的例子: #test FROM ubuntu MAINTAINER hello ENV MYDIR /mydir RUN m ...

  8. 17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump:

    17.1.1.5 Creating a Data Snapshot Using mysqldump 创建一个快照使用mysqldump: 创建一个数据快照的方式是使用mysqldump 工具来备份所有 ...

  9. TreePuzzle 一点感想

    题目链接 这一道题看起来像是一道贪心的水题,但是情况较难考虑周全,比如下图,我就考虑漏了这种情况,点0是可以移动到点1的.然后我就各种奇怪的分类讨论.最终还是没能A,决定放弃治疗. 然后我看了看解答, ...

  10. Struct初学的,页面跳转

    Filter控制器 jsp页面代码 <form action="page_login.action" method="post">     user ...