[Leetcode] LRU 算法实现
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是虚拟内存技术中,页置换时需要用到的算法,最近最少使用算法。也就是说替换掉最近最少被使用的页。
采用双链表实现一个栈,栈顶(用head指针表示)放最近使用的元素。end指针表示栈底,地方不够的时候end指针的元素值就会被覆盖,覆盖后因为成了最近使用,所以会被提到head的位置。链表中部也可能有元素被提到head的位置。
class LRUCache{
struct ListNode{
int key;
int value;
ListNode* prev;
ListNode* next;
};
public:
LRUCache(int capacity){
if(capacity >= ){
Capacity = capacity;
size = ;
}
} void set(int key, int value){
ListNode* p = head;
for(; p != NULL && p -> key != key; p = p -> next);
if(p == NULL && size == Capacity){ //The case that didn't find key and capacity is full
p = end;
p -> key = key;
}
if(p != NULL){ //The case that has found the key (treat the previous case as found the key in end)
p -> value = value;
if(p != head){ //If the key is in head of the list, we don't need to do anything since the head is the most recently used.
ListNode* pre = p -> prev;
ListNode* fol = p -> next; if(p == end && pre != NULL){
end = pre;
}
p -> next = head;
p -> prev = NULL;
head -> prev = p;
head = p; if(pre != NULL)
pre -> next = fol;
if(fol != NULL)
fol -> prev = pre;
}
}else{ //The case that has not found the key, and capacity is not full, need to create one.
p = new ListNode();
p -> key = key;
p -> value = value;
if(head == NULL){
head = end = p;
p -> prev = NULL;
p -> next = NULL;
}else{
p -> prev = NULL;
p -> next = head;
head -> prev = p;
head = p;
}
size++;
} } int get(int key){
ListNode* p = head;
for(; p != NULL && p -> key != key; p = p -> next);
if(p == NULL) return -; if(p != head){
ListNode* pre = p -> prev;
ListNode* fol = p -> next;
if(p == end)
end = pre; p -> next = head;
p -> prev = NULL;
head -> prev = p;
head = p; if(pre != NULL)
pre -> next = fol;
if(fol != NULL)
fol -> prev = pre;
}
return p -> value;
}
private:
ListNode* head = NULL;
ListNode* end = NULL;
int size = ;
int Capacity = ;
};
[Leetcode] LRU 算法实现的更多相关文章
- LRU算法的设计
一道LeetCode OJ上的题目,要求设计一个LRU(Least Recently Used)算法,题目描述如下: Design and implement a data structure for ...
- Leetcode:LRU Cache,LFU Cache
在Leetcode上遇到了两个有趣的题目,分别是利用LRU和LFU算法实现两个缓存.缓存支持和字典一样的get和put操作,且要求两个操作的时间复杂度均为O(1). 首先说一下如何在O(1)时间复杂度 ...
- LRU算法 - LRU Cache
这个是比较经典的LRU(Least recently used,最近最少使用)算法,算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. 一般应 ...
- LRU算法原理解析
LRU是Least Recently Used的缩写,即最近最少使用,常用于页面置换算法,是为虚拟页式存储管理服务的. 现代操作系统提供了一种对主存的抽象概念虚拟内存,来对主存进行更好地管理.他将主存 ...
- 如何实现LRU算法?
1.什么是LRU算法? LRU是一种缓存淘汰机制策略. 计算机的缓存容量有限,如果缓存满了就要删除一些内容,给新的内容腾位置.但是要删除哪些内容呢?我们肯定希望删掉那些没有用的缓存,而把有用的数据继续 ...
- 动手实现 LRU 算法,以及 Caffeine 和 Redis 中的缓存淘汰策略
我是风筝,公众号「古时的风筝」. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 那天我在 LeetCode 上刷到一道 LRU 缓存机制的问题, ...
- Redis 为何使用近似 LRU 算法淘汰数据,而不是真实 LRU?
在<Redis 数据缓存满了怎么办?>我们知道 Redis 缓存满了之后能通过淘汰策略删除数据腾出空间给新数据. 淘汰策略如下所示: 设置过期时间的 key volatile-ttl.vo ...
- Android图片缓存之Lru算法
前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...
- 缓存淘汰算法--LRU算法
1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是"如果数据最近被访问过,那么将来被访问的几率也 ...
随机推荐
- (转载)IE8+兼容经验小结
本文分享下我在项目中积累的IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的方式写的,然后下面的几点都关注过,那么基本上很大一部分IE8+兼容性问题都OK ...
- File Searching
Description Have you ever used file searching tools provided by an operating system? For example, in ...
- Thunder团队第二周 - Scrum会议7
Scrum会议7 小组名称:Thunder 项目名称:i阅app Scrum Master:杨梓瑞 工作照片: 参会成员: 王航:http://www.cnblogs.com/wangh013/ 李传 ...
- 由作业题引发对C++引用的一些思考
首先分析一段代码: #include <bits/c++config.h> #include <ostream> #include <iostream> #incl ...
- 让你的SilverLight程序部署在任意服务器上
是的,即使是免费的只支持HTML的空间,同样可以部署SilverLight应用.众所周知,SilverLight的部署问题其实就是.xap文件名是否能被服务器支持的问题.解决的方法无非就是添加MIME ...
- cacti安装spine 解决WARNING: Result from CMD not valid. Partial Result: U错误
安装spine用来替换cacti原本的执行方式,需要的包在附件中,请注意spine的安装包和你安装的cacti版本不用相同,最好是最新的spine 1.安装gcc #yum install -y gc ...
- table与div互相嵌套注意
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Redis 学习之数据类型
该文使用centos6.5 64位 redis-3.2.8 [root@localhost bin]# netstat -tunpl |grep 6379 查看redis 是否启动成功 一.Stri ...
- Java取两个变量不为空的变量的简便方法!
一.需求 最近在项目中遇到一个小问题,即从数据库取两个变量,判断取出的变量是否为空,取不为空的变量:若两个变量都不为空,取两个变量:两个变量都为空,则跳过: 二.解决方案(这里提供两种思路) 1.第一 ...
- 【bzoj1093】[ZJOI2007]最大半连通子图 Tarjan+拓扑排序+dp
题目描述 一个有向图G=(V,E)称为半连通的(Semi-Connected),如果满足:对于u,v∈V,满足u→v或v→u,即对于图中任意两点u,v,存在一条u到v的有向路径或者从v到u的有向路径. ...