LeetCode OJ:LRU Cache(最近使用缓存)
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.
最近使用缓存的问题,看到key和value感觉可能就要使用map了。用一个list维护一个cache,然后map指向相应cache的每个节点。
注意这里的最近使用,不管是get还是set都属于最近使用的范畴,代码如下所示:
class LRUCache{
struct cacheNode{
int key;
int val;
cacheNode(int k, int v):
key(k), val(v){}
};
public:
LRUCache(int capacity) {
size = capacity;
}
int get(int key) {
if(cacheMap.find(key) != cacheMap.end()){
auto it = cacheMap[key];
cacheList.splice(cacheList.begin(), cacheList, it);
cacheMap[key] = cacheList.begin();
return cacheList.begin()->val;
}else
return -;
}
void set(int key, int value) {
if(cacheMap.find(key) != cacheMap.end()){//list中存在该元素
auto it = cacheMap[key];
cacheList.splice(cacheList.begin(), cacheList, it);
cacheMap[key] == cacheList.begin();
cacheList.begin()->val = value;
}else{ //list中不存在该元素
if(cacheList.size() == this->size){
cacheMap.erase(cacheList.back().key);
cacheList.pop_back();
}
cacheList.push_front(cacheNode(key, value));
cacheMap[key] = cacheList.begin();
}
}
private:
int size;
list<cacheNode> cacheList;
unordered_map<int , list<cacheNode>::iterator> cacheMap;
};
LeetCode OJ:LRU Cache(最近使用缓存)的更多相关文章
- LeetCode之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- 【leetcode】LRU Cache
题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...
- 【leetcode】LRU Cache(hard)★
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 fol ...
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
随机推荐
- Swap交换分区--continue
Linux内核为了提高读写效率与速度,会将文件在内存中进行缓存,这部分内存就是Cache Memory(缓存内存).即使你的程序运行结束后,Cache Memory也不会自动释放.这就会导致你在Lin ...
- SQL.Mysql中Cast()函数的用法
比起orcale,MySQL相比之下就简单得多了,只需要一个Cast()函数就能搞定.其语法为:Cast(字段名 as 转换的类型 ),其中类型可以为: CHAR[(N)] 字符型 DATE 日期 ...
- 非root权限 安装更新gcc
本文主要参考网络上文章,并根据自己安装出现的问题进行补充. 参考文章: 1.gcc和boost的升级步骤(非root权限):https://blog.csdn.net/u010246947/artic ...
- 屏幕变黑白-winhotkey
下载了个windows hot key 的工具想看快捷键冲突 结果安装好之后屏幕变黑白了,变成辅助模式了.而且鼠标移动到哪都加蓝色框框 如果这个时候你带上耳机就能听到在朗读,这应该也是一种辅助模式 ...
- 20145327 《Java程序设计》第十周学习总结
20145327 <Java程序设计>第十周学习总结 教材学习内容总结 网络编程就是运行在不同计算机中两个程序之间的数据交换. 网络中的每个设备都会有一个唯一的数字标识,这个就是IP地址. ...
- 20135302魏静静——linux课程第七周实验及总结
linux课程第七周实验及总结 实验及学习总结 1. 编译链接的过程和ELF可执行文件格式(以hello为例) GNU编译系统编译源码: 首先,运行C预处理器(cpp),将.c文件翻译成.i文件——g ...
- cookie注入原理
cookie注入原理-->红客联盟 http://www.2cto.com/article/201202/118837.html 前言: document.cookie:表示当前浏览器中的coo ...
- c#结构体和字节流之间的相互转换
结构体转byte数组 1 首先要明白 ,是 在那个命名空间下 System.Runtime.InteropServices; 2 首先得到结构体的大小 2 开辟相应的内存空间 3 将结构体填 ...
- java中的垃圾回收机
任何语言在运行过程中都会创建对象,也就意味着需要在内存中为这些对象在内存中分配空间,在这些对象失去使用的意义的时候,需要释放掉这些内容,保证内存能够提供给新的对象使用.对于对象内存的释放就是垃圾回收机 ...
- JDBC 的 PreparedStatement 与 Statement
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...