原题地址

以前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. Hbase的Observer

    hbase提供了类似于触发器的组件observer,类似于存储过程的endpoint. hbase中的observer分别三类,MasterObserver.RegionObserver.WALObs ...

  2. AeroSpike 资料

    文档总览:http://www.aerospike.com/docs/ JAVA AeroSpike知识总览:http://www.aerospike.com/docs/client/java/sta ...

  3. wordpress学习-plugins-001

    plugins-插件 Akismet(Automattic Kismet)是应用广泛的一个垃圾留言过滤系统,其作者是大名鼎鼎的WordPress创始人Matt Mullenweg,Akismet也是W ...

  4. delphi常用函数过程

    数据类型转化 1.1.         数值和字符串转化 Procedure Str(X [: Width [ : Decimals ]]; var S); 将数值X按照一定格式转化成字符串S.Wid ...

  5. ASP.NET Web API安全认证

    http://www.cnblogs.com/codeon/p/6123863.html http://open.taobao.com/docs/doc.htm?spm=a219a.7629140.0 ...

  6. Android UI效果实现——Activity滑动退出效果

    更新说明: 1.在QQ网友北京-旭的提醒下,在SlideFrame的initilize方法中添加了focusable.focusableInTouch.clickable的状态设置,否则会导致部分情况 ...

  7. mq消息队列

    rabbitmq学习9:使用spring-amqp发送消息及同步接收消息 通过对spring-amqp看重要类的认识,下面来通过spring-amqp的发送消息及同步接收消息是如何实现的.有兴趣的朋友 ...

  8. oracle11g 新特性 - rman自动备份控制文件延迟

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 -6 ...

  9. 网站网页生成.shtml访问无法显示

    网站换了服务器后发现shtml网页无法访问,原因是没有注册.shtml扩展名,解决方法如下 IIS6.0解析shtm,shtml文件由于IIS6.0的安全性较以前有特别大的改进,所以在很多功能默认情况 ...

  10. Excel日期格式提取year

    Excel日期格式提取year =TEXT(YEAR(C2),"0000")