原题地址

以前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. mr的logs的查看

    在map或者reduce函数中使用System.out.println打印的信息沾满查看呢? 步骤1:启动history server /usr/local/hadoop-2.6.0/sbin/mr- ...

  2. 从零开始之ecshop基础篇(17)

    目标:基于自定义的mvc框架开发的案例(项目) 项目周期    需求分析 典型的业务逻辑:    电子商务:商城(京东),B2C,C2C(淘宝),团购,秒杀,代购 内容管理:新浪门户类,优酷视频管理, ...

  3. python 序列化之JSON和pickle详解

    JSON模块 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类 ...

  4. programming ruby

    ri #rdoc reader attr_reader attr_writer @@xx 类变量都是私有的 def 类名.xx end 类方法 [1,3,5,7].inject(0){|sum,e| ...

  5. 《大话设计模式》ruby版代码:简单工厂模式

    之前有看过<ruby设计模式>,不过渐渐的都忘记了.现在买了一个大话设计模式,看起来不是那么枯燥,顺便将代码用ruby实现了一下. # -*- encoding: utf-8 -*- #运 ...

  6. window7部署solr 4.7

    环境:win7 + tomcat 7.0.50 + solr 4.7 备注:C:\solr-4.7.0为solr.zip解压后的目录 C:\apache-tomcat-7.0.50为tomcat目录 ...

  7. 【转】Git常用命令备忘

    Git配置 git config --global user.name "robbin" git config --global user.email "fankai@g ...

  8. meteor icons & splash配置

    ionic resources http://ionicframework.com/docs/cli/icon-splashscreen.html 用一条指令生成ionic生需Icons & ...

  9. netlink

    http://blog.csdn.net/zirconsdu/article/details/8569193 http://www.xuebuyuan.com/1725837.html netlink ...

  10. iOS学习之Object-C语言简单的通讯录管理系统

    用这几天学的OC的知识,写了一个实现简单功能的通讯录管理系统,在这里分享给大家: 通讯录管理系统 *  需求: 1.定义联系人类Contact.实例变量:姓名(拼音,首字母大写).性别.电话号码.住址 ...