leetcode 146LRU cache
class LRUCache {
public:
LRUCache(int capacity) {_capacity=capacity;} //返回key对应的value
int get(int key){
auto it=cache.find(key);
if(it==cache.end()) return -;
update(it);//更改为最近访问
return it->second.first;//返回key值对应的value
}
void put(int key,int value){
auto it=cache.find(key);
if(it!=cache.end())
update(it);
else{
if(cache.size()==_capacity){
//删除缓存里最老的元素,cache和used中都删掉
cache.erase(used.back());
used.pop_back();
}
used.push_front(key);
}
cache[key]={value,used.begin()};
}
private:
//表示used,使用链表来存储近期使用过的值
typedef list<int> LI; //pair类型,第一个元素为value,第二个元素为value的值的存储位置;
typedef pair<int,LI::iterator> PII; //表示cache的key值和value的值和指针,
typedef unordered_map<int,PII>HIPII; HIPII cache;//定义一个cache
LI used;//定义一个最近访问的list(双端链表)
int _capacity; //将对应的cache节点更改到最近访问的属性
void update(HIPII::iterator it){
int key=it->first; used.erase(it->second.second);//删除该元素,通过指针(迭代器),这也是保留LI::iterator的原因
used.push_front(key);//将该元素列为最新使用元素
//以指针指向的位置作为cache与used之间的联系;
it->second.second=used.begin();//将最近使用的最新元素的位置存储到PII类型的第二个元素中
}
};
一个更整齐的版本:
/******
//定义三个private数据,LRU尺寸,LRU pair<key,value>, LRU map<key,iterator of pair> //利用splice操作erase,makepair 等完成LRUcache //put()
如果m中有对应的key,value,那么移除l中的key,value
如果m中没有,而size又==cap,那么移除l最后一个key,value,并移除m中对应的key,iterator 在l的开头插入key,value,然后m[key]=l.begin(); ****/
class LRUCache {
public:
LRUCache(int capacity){
cap=capacity;
}
int get(int key){
unordered_map<int,list<pair<int,int>>::iterator>::iterator it=m.find(key);
if(it==m.end()) return -;
l.splice(l.begin(),l,it->second);
return it->second->second;
}
void put(int key,int value){
auto it=m.find(key);
if(it!=m.end()) l.erase(it->second);
if(l.size()==cap){
int k=l.rbegin()->first;
l.pop_back();
m.erase(k);//map可以根据key值和迭代器值移除,查找
}
l.push_front(make_pair(key,value));
m[key]=l.begin();
} private:
int cap;//LRU size
list<pair<int,int>>l;//pair<key,value>
unordered_map<int,list<pair<int,int>>::iterator>m;//unordered_map<key,key&value's pair iterator>
}; /**
* Your LRUCache object will be instantiated and called as such:
* LRUCache* obj = new LRUCache(capacity);
* int param_1 = obj->get(key);
* obj->put(key,value);
*/
leetcode 146LRU cache的更多相关文章
- [LeetCode]题解(python):146-LRU Cache
题目来源: https://leetcode.com/problems/lru-cache/ 实现一个LRU缓存.直接上代码. 代码(python): class LRUCache(object): ...
- [LeetCode] LFU Cache 最近最不常用页面置换缓存器
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- [LeetCode] LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Leetcode: LFU Cache && Summary of various Sets: HashSet, TreeSet, LinkedHashSet
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- LeetCode LFU Cache
原题链接在这里:https://leetcode.com/problems/lfu-cache/?tab=Description 题目: Design and implement a data str ...
- LeetCode OJ-- LRU Cache ***@
https://oj.leetcode.com/problems/lru-cache/ 涨姿势的一道题,相当于实现一种数据结构,实现最近最少使用数据结构. // 用来记录 前后节点都是什么 类似链表上 ...
- LeetCode:LRU Cache
题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...
- LeetCode——LRU Cache
Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...
随机推荐
- PropertySource顺序
Spring Boot使用一个非常特殊的PropertySource顺序,该顺序旨在允许合理地覆盖值.按以下顺序考虑属性: $HOME/.config/spring-boot当devtools处于活动 ...
- MySQL查询一张表有多少个字段
SQL如下 select count(*) from information_schema.COLUMNS where TABLE_SCHEMA='数据库名' and table_name='表名'
- 4.pca与梯度上升法
(一)什么是pca pca,也就是主成分分析法(principal component analysis),主要是用来对数据集进行降维处理.举个最简单的例子,我要根据姓名.年龄.头发的长度.身高.体重 ...
- 模块之-os模块
模块之-os模块 >>> import os >>> os.getcwd() #获取当前工作目录 'C:\\Users\\Administrator' >&g ...
- 学习Linux的准备
学习方式: 主动学习: 动手实践:40% 讲给别人:70% 被动学习: 听课:10% 笔记:20% 写博客的要求: 写博客是对某一方面知识的总结,输出:是知识的书面化的表达方式.写博客不同于写笔记,笔 ...
- spring中对JDO的配置
<!-- persistenceManagerFactory --> <bean id="myPmf" class="org.springframewo ...
- js 简单实现隐藏和显示
<html> <head> <meta charset="gb2312"> <title>隐藏和显示</title> & ...
- cmd中subst的使用
SUBST [drive1: [drive2:]path] drive1: 指定要指派路径的虚拟驱动器.[drive2:]path 指定物理驱动器和要指派给虚拟驱动器的路径. 路径替换 ...
- [全局最小割][Stoer-Wagner 算法] 无向图最小割
带有图片例子的 [BLOG] 复杂度是$(n ^ 3)$ HDU3691 // #pragma GCC optimize(2) // #pragma GCC optimize(3) // #pragm ...
- 【未知来源】K-th String
题意 求有多少种 前 \(n\) 个小写字母的排列 \(t\),满足其所有子串按字典序从小到大排列,第 \(k\) 个子串是一个给定字符串 \(s\).答案模 \(10^9+7\). \(1\le n ...