leetcode 之LRU Cache(26)

很实际的一道题。定义一个双向链表list,方便插入和删除;定义一个哈希表,方便查找。
具体的,哈希表存放每个结点的key和它对应的结点的地址;访问结点时,如果结点存在,则将其交换到头部,同是更新哈希表中的地址;
插入结点时,首先判断capacity是否达到了上限,如果是则在链表和哈希表中删除该结点;新结点插入链表头部。
有很多细节需要注意,双向链表和哈希表的用法,需要多加体会。
class LRUCache
{
private:
struct CacheNode
{
int key;
int value;
CacheNode(int k, int v) :key(k), value(v){}
};
int capacity;
list<CacheNode> cacheList;
unordered_map<int, list<CacheNode>::iterator> cacheMap;
public:
LRUCache(int capacity)
{
this->capacity = capacity;
} int get(int key)
{
if (cacheMap.find(key) == cacheMap.end())return -;
//将当前访问的节点移到头部
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
cacheMap[key] = cacheList.begin(); return cacheMap[key]->value;
} void set(int key, int value)
{
//如果该结点不存在
if (cacheMap.find(key) == cacheMap.end())
{
if (cacheList.size() == capacity)
{
//删除最少访问结点即表尾结点
cacheMap.erase(cacheList.back().key);
cacheList.pop_back();
}
//插入新结点
cacheList.push_front(CacheNode(key, value));
cacheMap[key] = cacheList.begin();
} else
{
cacheMap[key]->value = value;
cacheList.splice(cacheList.begin(), cacheList, cacheMap[key]);
cacheMap[key] = cacheList.begin();
}
}
};
leetcode 之LRU Cache(26)的更多相关文章
- 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 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- 【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
- [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 ...
- 【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 ...
随机推荐
- CentOS 装hadoop3.0.3 版本踩坑
1.but there is no HDFS_NAMENODE_USER defined. Aborting operation. [root@xcff sbin]# ./start-dfs.sh S ...
- BZOJ1588:[HNOI2002]营业额统计——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1588 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务 ...
- [Leetcode] Binary tree postorder traversal二叉树后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...
- oracle中varchar2字段存入blob字段及blob转成varchar2
CREATE OR REPLACE FUNCTION C2B (b IN CLOB default empty_clob()) RETURN BLOB -- typecasts BLOB to CLO ...
- 【树形DP】【UVA10859】 Placing Lampposts
传送门 Description 给定一个\(n\)个点\(m\)条边的无向无环图,选择尽量少的节点,使得所有边都至少有一个顶点被选择.在这个基础上,要求有两个顶点被选择的边数尽可能大 Input 多组 ...
- iOS更改项目名称的详细步骤
http://www.cocoachina.com/ios/20150104/10824.html
- HDU 5538 (水不水?)
House Building Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others) ...
- 洛谷P3396 哈希冲突 (分块)
洛谷P3396 哈希冲突 题目背景 此题约为NOIP提高组Day2T2难度. 题目描述 众所周知,模数的hash会产生冲突.例如,如果模的数p=7,那么4和11便冲突了. B君对hash冲突很感兴趣. ...
- 树莓派使用Samba共享文件夹
转载自:http://raspberrypihq.com/how-to-share-a-folder-with-a-windows-computer-from-a-raspberry-pi/ Shar ...
- Using CORS(译)
原文地址:https://docs.webplatform.org/wiki/tutorials/using_cors 总结 一篇对"Cross Origin Resource Sharin ...