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的更多相关文章

  1. [LeetCode]题解(python):146-LRU Cache

    题目来源: https://leetcode.com/problems/lru-cache/ 实现一个LRU缓存.直接上代码. 代码(python): class LRUCache(object): ...

  2. [LeetCode] LFU Cache 最近最不常用页面置换缓存器

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  3. [LeetCode] LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  4. 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 ...

  5. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  6. LeetCode LFU Cache

    原题链接在这里:https://leetcode.com/problems/lfu-cache/?tab=Description 题目: Design and implement a data str ...

  7. LeetCode OJ-- LRU Cache ***@

    https://oj.leetcode.com/problems/lru-cache/ 涨姿势的一道题,相当于实现一种数据结构,实现最近最少使用数据结构. // 用来记录 前后节点都是什么 类似链表上 ...

  8. LeetCode:LRU Cache

    题目大意:设计一个用于LRU cache算法的数据结构. 题目链接.关于LRU的基本知识可参考here 分析:为了保持cache的性能,使查找,插入,删除都有较高的性能,我们使用双向链表(std::l ...

  9. LeetCode——LRU Cache

    Description: Design and implement a data structure for Least Recently Used (LRU) cache. It should su ...

随机推荐

  1. linux ftp 添加用户及权限管理

    Linux下创建用户是很easy的事情了,只不过不经常去做这些操作,时间久了就容易忘记,顺便配置一下FTP.声明:使用Linux版本release 5.6,并以超级管理员root身份运行. 1.创建用 ...

  2. Linux运维课程体系大纲

    Linux入门:    Linux系统管理:    Linux服务及安全管理:        httpd,lamp,lnmp        Cache:memcached,varnish(缓存系统)  ...

  3. AttributeError: module 'scipy.misc' has no attribute 'imread'

    运行python程序报错:AttributeError: module 'scipy.misc' has no attribute 'imread' 报错原因1:scipy版本过高 解决方案:降低sc ...

  4. keras训练大量数据的办法

    最近在做一个鉴黄的项目,数据量比较大,有几百个G,一次性加入内存再去训练模青型是不现实的. 查阅资料发现keras中可以用两种方法解决,一是将数据转为tfrecord,但转换后数据大小会方法不好:另外 ...

  5. 【BZOJ1176】Mokia

    题目大意:给定一个 N*N 的矩形,有 Q 次操作,每个操作可以是矩形单点修改或查询子矩形的权值和. 题解:CDQ分治适合处理修改操作之间互不影响且支持离线的题目. 满足以上操作条件的显然可以树套树来 ...

  6. [洛谷P4072] SDOI2016 征途

    问题描述 Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地.除第m天外,每一天晚上Pine都必须在休息站过夜.所以,一段路 ...

  7. http的get与post

    1.http请求 http有两种报文,请求报文 (发送请求,可能包含数据)和响应报文(服务器响应请求获取数据).一个http请求报文由请求行,请求头部,空行和请求正文(数据)四个部分组成. HTTP请 ...

  8. vue自定义指令,自动调用下载的方法

    directives: { clickDown: { inserted (el, binding, item) { if (+binding.value.item.fromId === +item.c ...

  9. nginx实现商品详情页的缓存

  10. C# checkedlistbox 控件 有bug

    加入集合 private void Form2_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Column ...