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 ...
随机推荐
- Java 计算两点间的全部路径(二)
一.有向线段,存储开始点与结束点 /** * 有方向的线段 * * @author Gm * */ public class DirectionLine implements Cloneable { ...
- Js 将图片的绝对路径转换为base64编码
转.... 我们可以使用canvas.toDataURL的方法将图片的绝对路径转换为base64编码:在这我们引用的是淘宝首页一张图片如下: var img = "https://img. ...
- symfony3 使用命令行工具生成Entity实体所踩的坑
1.把配置文件汇总连接邮箱的配置信息注释掉了,在创建Entity时php bin/console doctrine:generate:entity报错 2. 错误原因是实体文件映射到数据库中的字段时候 ...
- zencart模板列表下载地址
下载index.html文件后用浏览器打开,里面有一百多个zencart模板示例 下载地址:zencart模板示例下载地址 或者复制下面网址,用浏览器打开即可下载: http://bcs.duapp. ...
- 织梦网站dedecms防止挂马的思路
DedeCms做为国内使用最为广泛使用人数最多的CMS之一,经常爆出漏洞,每个漏洞的爆出,影响都是一大片,轻则被人挂广告.弹框,重则服务器成为肉机,宝贵数据丢失.那么有什么办法可以提高DedeCms的 ...
- hdu3586 Information Disturbing[二分答案+树形DP]
给定 n 个节点的树,边有权值.1 号点是根,除了 1 号点外的度数为 1 的节点是叶子.要求切断所有叶子和 1 号点之间的联系,切断一条边要花费这条边上权值对应的代价,要求总的代价不超过 m.在满足 ...
- js callback回调的一种写法
getLocation.cityname(latitude, longitude, function (data1) { SetCityCallBack(data1); }); 定义方法: var g ...
- cdh本地源安装-自用
yum 安装cm 参考官网 主机名 /etc/hosts hostnamectl set-hostname foo-1.data.com hostnamectl set-hostname foo-2. ...
- 线上 CPU100% 排查方案
问题:生产服务器上部署了几个java程序,突然出现了CPU100%的异常告警,你如何定位出问题呢? 下面给出两种系统下的排查步骤,都是一模一样的,只是命令稍有区别! 查消耗cpu最高的进程PID 根据 ...
- centos 修改时区以及修正时间
1.查看系统当前的时区 [app@127-0-0-1 shine]$ timedatectl Local time: Wed 2019-10-23 17:56:17 CST Universal tim ...