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 ...
随机推荐
- 整理一些《纸书科学计算器》的小Tips
本文最开始是在2016年的文章 Win10应用<纸书科学计算器>更新啦! 发表之后撰写的,当时那篇文章收到了不少人点赞,应用在国内市场的日下载量也突然上涨,让我感到受宠若惊,这里要感谢Wp ...
- path.resolve()和path.join()
resolve 作用:path.resolve() 该方法将一些的 路径/路径段 解析为绝对路径. 语法:path.resolve([...paths]) 说明: ...paths <strin ...
- 自动配置@Resource与@Autowired
总结自:https://www.cnblogs.com/kuotian/p/8795812.html 试用情形:bean的某个成员变量是另一个bean 如果使用配置: <bean id=&quo ...
- vim 中查询和转换编码
vim中查询修改文件编码格式 set fileencoding 查看现在文本的编码 :set fenc=编码 转换当前文本的编码为指定的编码 :set enc=编码 以指定的编码显示文本,但不保存到文 ...
- linux如何以十六进制格式来查看任意文件
答:vim+xxd 使用方法如下: 1.vim -b file.txt 2.在vim的命令行模式下对文件进行16进制转换 输入:%!xxd 3.在vim的命令行模式下回到正常格式 输入:%!xxd - ...
- CentOS(64位)安装apr
安装apr来提高tomcat 的可伸缩性和性能 cd /usr/local/ 1. 下载apr 和 apr-util最新版 wget http://apache.fayea.com/apache-m ...
- C# Page基础功能,用于各页面继承
IBasePage.cs文件 /// <summary> /// 用于页面或用户控件 /// </summary> public interface IBasePage { / ...
- POJ1247-Magnificent Meatballs
http://poj.org/problem?id=1247 Magnificent Meatballs Time Limit: 1000MS Memory Limit: 10000K Total ...
- C++ substr 和 substring
功能相似,但参数不同 substr(start,length); substring(start,end);
- python selenium常用基本方法---H5和键盘鼠标操作
一.模拟手机打开页面(H5测试) from selenium import webdriver mobile_emulation = {'deviceName':'iPhone X'} options ...