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. deep_learning_Function_matpotlib_scatter()函数

    plt.scatter()函数用于生成一个scatter散点图. matplotlib.pyplot.scatter(x, y, s=20, c='b', marker='o', cmap=None, ...

  2. IIS 程序池优化配置方案

    内容目录 IIS 程序池优化配置方案IIS高并发配置一.IIS站点绑定程序池设置二.支持万级并发请求 IIS 程序池优化配置方案 最近由于系统的客户越来越多,有客户反映访问速度变慢,尤其是api的请求 ...

  3. RHEL6中LVM逻辑卷管理

    1.LVM 基本术语   物理卷(physical volume):物理卷在逻辑卷管理中处于最底层,它可以是实际物理硬盘上的分区,也可以是整个物理硬盘.   卷组(Volume Group):卷组建立 ...

  4. Hive Serde(四)

    Hive Serde 目的: ​ Hive Serde用来做序列化和反序列化,构建在数据存储和执行引擎之间,对两者实现解耦. 应用场景: ​ 1.hive主要用来存储结构化数据,如果结构化数据存储的格 ...

  5. css 浮动的知识点

    首先要知道,div是块级元素,在页面中独占一行,自上而下排列,也就是传说中的流.如下图: 可以看出,即使div1的宽度很小,页面中一行可以容下div1和div2,div2也不会排在div1后边,因为d ...

  6. Linux查看所有子文件夹及文件的数量

    find命令查看(推荐): 所有子目录的数量: [root@localhost ~]# find afish -type d | wc -l158[root@localhost ~]# find af ...

  7. PHP的函数获取图片的宽高等信息

    PHP的函数getimagesize可以得到图片的宽高等信息 array getimagesize ( string $filename [, array &$imageinfo ] )   ...

  8. 如何在当前目录下打开Windows cmd?

    在当前目录下,按Alt + D (全选当前目录),然后输入 cmd 再按回车 Enter .

  9. ArrayMatched

    import os from jinja2 import Environment,FileSystemLoader def generateNewLackArray(ArrayList,count,T ...

  10. Linux性能分析命令工具汇总

    转自:http://rdc.hundsun.com/portal/article/731.html?ref=myread 出于对Linux操作系统的兴趣,以及对底层知识的强烈欲望,因此整理了这篇文章. ...