原题地址

以前Leetcode的测试数据比较弱,单纯用链表做也能过,现在就不行了,大数据会超时。通常大家都是用map+双向链表做的。

我曾经尝试用C++的list容器来写,后来发现map没法保存list的iterator,总是报错,我也不知道为什么。后来只好手写双向链表,真是痛苦啊,一不小心就会出错。怪不得这道题是hard

代码:

 struct Node {
int key;
int val;
Node *prev;
Node *next;
Node() : prev(NULL), next(NULL) {}
Node(int k, int v) : key(k), val(v), prev(NULL), next(NULL) {}
}; class LRUCache {
public:
map<int, Node*> index;
Node *head;
Node *tail;
int maxSize; LRUCache(int capacity) {
maxSize = capacity;
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
} int get(int key) {
if (index.find(key) == index.end())
return -; Node *node = index[key];
node->prev->next = node->next;
node->next->prev = node->prev;
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node; return node->val;
} void set(int key, int value) {
if (maxSize == )
return; if (index.find(key) != index.end()) {
Node *node = index[key];
node->val = value;
node->prev->next = node->next;
node->next->prev = node->prev;
node->prev = head;
node->next = head->next;
head->next->prev = node;
head->next = node;
}
else {
Node *node = new Node(key, value);
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
index[key] = node;
if (index.size() > maxSize) {
node = tail->prev;
tail->prev = node->prev;
node->prev->next = tail;
index.erase(node->key);
delete node;
}
}
}
};

Leetcode#146 LRU Cache的更多相关文章

  1. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

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

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

  3. Java for LeetCode 146 LRU Cache 【HARD】

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

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

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

  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 ----- java

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

  7. 【LeetCode】146. LRU Cache 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...

  8. 【LeetCode】146. LRU Cache

    LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...

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

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

随机推荐

  1. 《Google 代码风格指南》

    <Google 代码风格指南> https://github.com/google/styleguide

  2. C++求最小公倍数

    题目内容:求两个正整数的最小公倍数. 输入描述:输入数据含有不多于50对的数据,每对数据由两个正整数(0<n1,n2<100000)组成. 输出描述:对于每组数据n1和n2,计算最小公倍数 ...

  3. Android--将图片存放到我们本地

    代码里面有详细的解释,我就不多说了 //处理并保存图像 private File dealPhoto(Bitmap photo){ FileOutputStream fileOutputStream ...

  4. hdu 4941 Magical Forest

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4941 Magical Forest Description There is a forest can ...

  5. Android保存图片到系统图库

    最近有些用户反映保存图片之后在系统图库找不到保存的图片,遂决定彻底查看并解决下. Adnroid中保存图片的方法可能有如下两种: 第一种是自己写方法,如下代码: public static File ...

  6. "奇葩家园“之genymotion工具篇

    genymotion 简直就是android开发者的福音,比android内置的模拟器不知道快多少, 具体的安装可以参考如下: 1.登陆官方网站,必须先注册 https://www.genymotio ...

  7. 【EF Code First】 一对一、一对多的多重关系配置

    这里使用相册Album和图片Picture的关系做示例 1,Album与Picture最基本的关系是1-n(一个相册可以有多张图片) 这时Album.Picture实体类可以这么定义 /// < ...

  8. 从零开始学ios开发(三):第一个有交互的app

    感谢大家的关注,也给我一份动力,让我继续前进.有了自己的家庭有了孩子,过着上有老下有小的生活,能够挤出点时间学习真的很难,每天弄好孩子睡觉已经是晚上10点左右了,然后再弄自己的事情,一转眼很快就到12 ...

  9. SQL-LINQ-Lambda语法对照,好记性不如烂笔头

    忘记的时候就翻阅翻阅吧~~ SQL LINQ Lambda SELECT *FROM HumanResources.Employee from e in Employees select e Empl ...

  10. firebreath 在谷歌和火狐浏览器下的调试 以及打包

    在寻找插件开发资料的过程中找到了一个开发浏览器插件的开源项目——firebreath firebreath的安装以及测试我就不再叙述了,可以参考大神的文章 . http://www.blogjava. ...