LeetCode: LRU Cache [146]
【题目】
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]的更多相关文章
- [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 (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 [Forward]
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 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 ...
随机推荐
- spark sql 基本用法
一.通过结构化数据创建DataFrame: publicstaticvoid main(String[] args) { SparkConf conf = new SparkConf() .se ...
- 在vmware里面免费安装纯净的xp虚拟机
1. 安装vmware, 略 2. 下载xp http://msdn.itellyou.cn/ 用迅雷下载Windows XP Professional with Service Pack 3 (x8 ...
- Android空指针异常的常见情况
把我经常遇到的nullpoitException写在这里,以便以后自己查找原因. 1.用findViewById(param )的方法获取一个view对象的时候,有的时候其实应该是获取一个layout ...
- Swing布局管理器
在Swing中使用的所有布局管理器都可以实现LayoutManager接口.Swing中有五种常见的布局管理器分别为FlowLayout.BorderLayout.GridLayout.CardLay ...
- Ural 1450 求最长路 SPFA
题意就是求S点到T点的有向无环图中的最长路. 用SPFA可以解决. 一开始一直RE的原因 QAQ 竟然是在开Edge 邻接表的时候开小了 改了一下4Y #include <stdio.h> ...
- iOS- 三步快速集成社交化分享工具ShareSDK
http://www.cnblogs.com/qingche/p/3727559.html 1.前言 作为现在App里必不可少的用户分享需要,社交化分享显然是我们开发app里较为常用的. 最近因为公司 ...
- poj-3899-The Lucky Numbers 模拟+数学
题目链接: http://poj.org/problem?id=3899 题目意思: 求给定区间内,只含4.7的数的个数以及通过反转后在该区间内的个数和. 解题思路: 模拟+数学. 代码解释的很详细, ...
- EXT.NET高效开发(三)——使用Chrome浏览器的开发人员工具
这篇帖子老少皆宜,不分男女,不分种族,不分职业.俗话说:“磨刀不误砍柴工”.掌握一些开发工具的使用,对自己帮助是很大的(无论是用于分析问题,还是提高生产力).本篇就讲述如何利用Chrome浏览器(这里 ...
- [转]SELinux管理与配置
原文链接:http://blog.csdn.net/huangbiao86/article/details/6641893 1.1 SElinux概述 SELinux(Security-Enhance ...
- java--随机数的产生
随机数产生的三种方法: 1.system.currentTimeMillis() public class Demo1{ public static void main(String[] args) ...