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 ...
随机推荐
- 如何提升集群资源利用率? 阿里容器调度系统Sigma 深入解析
阿里妹导读:为了保证系统的在线交易服务顺利运转,最初几年,阿里都是在双11大促来临之前大量采购机器储备计算资源,导致了双11之后资源大量闲置点现象.是否能把计算任务与在线服务进行混合部署,在现有弹性资 ...
- POJ1474:Video Surveillance——题解
http://poj.org/problem?id=1474 题目大意:给按照顺时针序的多边形顶点,问其是否有内核. —————————————————————————————— (和上道题目一模一样 ...
- BZOJ2301:[HAOI2011]Problem b——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=2301 https://www.luogu.org/problemnew/show/P2522 对于给 ...
- BZOJ1878:[SDOI2009]HH的项链——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1878 题面源于洛谷 题目背景 无 题目描述 HH 有一串由各种漂亮的贝壳组成的项链.HH 相信不同的 ...
- 【DP】【Uva437】UVA437 The Tower of Babylon
传送门 Description Input Output Sample Input Sample Output Case : maximum height = Case : maximum heigh ...
- 使用openssl进行文件加密
#include <iostream> #include <string> #include <stdlib.h> using namespace std; int ...
- PowerDesigner 技巧【1】
Name与Code同步的问题: PowerDesigner中,修改了某个字段的name,其code也跟着修改,这个问题很讨厌,因为一般来说,name是中文的,code是字段名. 解决方法如下: 1.选 ...
- hdu 1754 线段树 单点更新 动态区间最大值
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- ssm项目,web容器无法初始化项目
在web.xml中配置加载spring时,发现项目无法运行:而去掉spring的配置时,项目可以被初始化. 此时应考虑到spring的配置文件中存在错误,以至于web容器无法对项目成功初始化,在web ...
- Kubernetes - Start containers using Kubectl
In this scenario, you'll learn how to use Kubectl to create and launch Deployments, Replication Cont ...