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 ...
随机推荐
- Python日期字符串比较
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl 转载请注明出处 需要用python的脚本来快速检测一个文件内的二个时间日期字符串的大小,其实实现很简单,首先一些 ...
- zookeeper 监听事件 PathChildrenCacheListener
zookeeper 监听事件 PathChildrenCacheListener PathChildrenCacheListener一次父节点注册,监听每次子节点操作,不监听自身和查询. 1.测试类: ...
- java单例模式之懒汉式分析
转自:http://blog.csdn.net/withiter/article/details/8140338 今天中午闲着没事,就随便写点关于Java单例模式的.其实单例模式实现有很多方法,这里我 ...
- JS正则表达式从入门到入土(6)—— 贪婪模式与非贪婪模式
贪婪模式 之前说了正则的量词,但是量词会带来一个到底该匹配哪个的问题. 如下正则表达式: \d{3,6} 这个正则表达式是匹配3到6个数字,但是当这个正则表达式被用来匹配12345678这个字符串,到 ...
- NOIP2019前的训练记录
\(April\):肛多项式,学\(FWT\)一个小时无果后背了六个公式,看来证明又得咕很久了
- 优雅的处理你的Java异常
本文介绍 本文仅按照业务系统开发角度描述异常的一些处理看法.不涉及java的异常基础知识,可以自行查阅 <Java核心技术 卷I> 和 <java编程思想> 可以得到更多的基础 ...
- C#生成PDF2019
因接口生成Pdf推送, 工作需要进行Pdf生成,但网上生成Pdf的文档好少: 1.生成Pdf需要文件路径/内容 都可以配置 2.使用组件 itextsharp.dll 本人用版本:v2.0.5072 ...
- Jenkins 集成Ansible教程
前提条件: 1. 部署Jenkins Server 2. 部署 Ansible Server 一.在Jenkins安装SSH插件 系统管理 -> 插件管理 二.在Jenkins 凭证中添加Ans ...
- How To Use Coordinates To Extract Sequences In Fasta File
[1] bedtools (https://github.com/arq5x/bedtools2) here is also bedtools (https://github.com/arq5x/be ...
- Docker 学习记录
docker logs 查看日志 docker logs 容器id docker logs -f 容器id 这次命令后面添加了一个新的标识 -f. 和 tail -f 类似, docker logs ...