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 算法实现的更多相关文章

  1. LRU算法的设计

    一道LeetCode OJ上的题目,要求设计一个LRU(Least Recently Used)算法,题目描述如下: Design and implement a data structure for ...

  2. Leetcode:LRU Cache,LFU Cache

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

  3. LRU算法 - LRU Cache

    这个是比较经典的LRU(Least recently used,最近最少使用)算法,算法根据数据的历史访问记录来进行淘汰数据,其核心思想是“如果数据最近被访问过,那么将来被访问的几率也更高”. 一般应 ...

  4. LRU算法原理解析

    LRU是Least Recently Used的缩写,即最近最少使用,常用于页面置换算法,是为虚拟页式存储管理服务的. 现代操作系统提供了一种对主存的抽象概念虚拟内存,来对主存进行更好地管理.他将主存 ...

  5. 如何实现LRU算法?

    1.什么是LRU算法? LRU是一种缓存淘汰机制策略. 计算机的缓存容量有限,如果缓存满了就要删除一些内容,给新的内容腾位置.但是要删除哪些内容呢?我们肯定希望删掉那些没有用的缓存,而把有用的数据继续 ...

  6. 动手实现 LRU 算法,以及 Caffeine 和 Redis 中的缓存淘汰策略

    我是风筝,公众号「古时的风筝」. 文章会收录在 JavaNewBee 中,更有 Java 后端知识图谱,从小白到大牛要走的路都在里面. 那天我在 LeetCode 上刷到一道 LRU 缓存机制的问题, ...

  7. Redis 为何使用近似 LRU 算法淘汰数据,而不是真实 LRU?

    在<Redis 数据缓存满了怎么办?>我们知道 Redis 缓存满了之后能通过淘汰策略删除数据腾出空间给新数据. 淘汰策略如下所示: 设置过期时间的 key volatile-ttl.vo ...

  8. Android图片缓存之Lru算法

    前言: 上篇我们总结了Bitmap的处理,同时对比了各种处理的效率以及对内存占用大小.我们得知一个应用如果使用大量图片就会导致OOM(out of memory),那该如何处理才能近可能的降低oom发 ...

  9. 缓存淘汰算法--LRU算法

    1. LRU1.1. 原理 LRU(Least recently used,最近最少使用)算法根据数据的历史访问记录来进行淘汰数据,其核心思想是"如果数据最近被访问过,那么将来被访问的几率也 ...

随机推荐

  1. python SyntaxError: Non-ASCII character '\xe8' in file C:\Users\nwpujun\PycharmProjects\projects\hrl1\hrlAgent\src\li_nn.py on line 52

    解决方法:在文件头部加上这样的一句话 # -*- coding: utf-8 -*- 注意:加在其他的位置可能没用,我就是这样的

  2. Hyperledger fablic 1.0 在centos7环境下的安装与部署和动态增加节点

    Hyperledger fablic 1.0 在centos7环境下的安装与部署和动态增加节点 一.安装docker 执行代码如下: curl -sSL https://get.daocloud.io ...

  3. jQuery File Upload文件上传插件简单使用

    前言 开发过程中有时候需要用户在前段上传图片信息,我们通常可以使用form标签设置enctype=”multipart/form-data” 属性上传图片,当我们点击submit按钮的时候,图片信息就 ...

  4. Alpha阶段中间产物——Thunder团队

    Part One 版本控制 git地址:https://git.coding.net/lick468/iReader.git Part Two 软件功能说明书 相关链接:http://www.cnbl ...

  5. ASP.NET MVC中controller和view相互传值的方式

    ASP.NET MVC中Controller向view传值的方式: ViewBag.ViewData.TempData 单个值的传递 Json 匿名类型 ExpandoObject Cookie Vi ...

  6. Delegate(QLabel和QComboBox)

    一.最终效果 二.实现思路 1.createEditor()中create两个控件,分别是QLabel和QComboBox,将其添加到一个widget中,然后返回该widget: 2.setEdito ...

  7. 福大软工1816:Alpha(7/10)

    Alpha 冲刺 (7/10) 队名:Jarvis For Chat 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.完成 ...

  8. java — 设计模式

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代码可靠性. 一.设计模式的分类 ...

  9. lintcode-151-买卖股票的最佳时机 III

    151-买卖股票的最佳时机 III 假设你有一个数组,它的第i个元素是一支给定的股票在第i天的价格.设计一个算法来找到最大的利润.你最多可以完成两笔交易. 注意事项 你不可以同时参与多笔交易(你必须在 ...

  10. 最近面试js部分试题总结

    二,JavaScript面试题总结 1,首先是数组去重算法:给一个数组,去掉重复值 (function() { var arr = [1, 2, 3, 3, 4, ]; function unique ...