很实际的一道题。定义一个双向链表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)的更多相关文章

  1. Java for LeetCode 146 LRU Cache 【HARD】

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

  2. LeetCode之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  3. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  4. 【LeetCode】LRU Cache 解决报告

    插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...

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

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

  6. [LeetCode] 146. LRU Cache 近期最少使用缓存

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

  7. 【leetcode】LRU Cache

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

  8. 【leetcode】LRU Cache(hard)★

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

  9. 【Leetcode】 LRU Cache实现

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

  10. leetcode 146. LRU Cache ----- java

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

随机推荐

  1. BZOJ2301:[HAOI2011]Problem b——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=2301 https://www.luogu.org/problemnew/show/P2522 对于给 ...

  2. bzoj2733: [HNOI2012]永无乡(splay+启发式合并/线段树合并)

    这题之前写过线段树合并,今天复习Splay的时候想起这题,打算写一次Splay+启发式合并. 好爽!!! 写了长长的代码(其实也不长),只凭着下午的一点记忆(没背板子...),调了好久好久,过了样例, ...

  3. ubuntu16.04登录后无dash,无启动栏launch,无menu bar,只有桌面背景解决办法

    今天打开电脑,与往常一样输入用户名密码登录后,发现桌面上空空如也,启动栏launch,menu bar什么的都消失了,桌面上文件可以打开,但是无法拖动位置,无法关闭(因为menu bar没了,无法鼠标 ...

  4. ACE中TCP通信

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/07/585095.html 概述: 传输控制协议TCP(Transmission Contro ...

  5. Codeforces Round #326 (Div. 2) B Duff in Love 简单数论 姿势涨

    B. Duff in Love time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  6. AtCoder Grand Contest 031 B - Reversi(DP)

    B - Reversi 题目链接:https://atcoder.jp/contests/agc031/tasks/agc031_b 题意: 给出n个数,然后现在你可以对一段区间修改成相同的值,前提是 ...

  7. oracle xml操作

      /*=====================生成\修改xml========================= */ --xmlelement多个标签层级 SELECT XMLELEMENT(& ...

  8. 游戏编程入门之测试Xbox360控制输入

    代码: #include<Windows.h> #include<d3d9.h> #include<d3dx9.h> #include<Xinput.h> ...

  9. String作为输出型参数时获取不到值

    有时候在一个方法中,我们需要返回多个字符串,而又不想将这些字段包成一个类.此时就需要使用输出型参数. 但是如果将输出型参数的类型声明为String,那么调用该方法后,是获取不到我们想要的值的. 测试代 ...

  10. [LeetCode] 14. Longest Common Prefix ☆

    Write a function to find the longest common prefix string amongst an array of strings. 解法: 广度优先搜索:先比 ...