Leetcode#146 LRU Cache
以前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的更多相关文章
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 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@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- [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 ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- 【LeetCode】146. LRU Cache 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...
- 【LeetCode】146. LRU Cache
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...
- LeetCode之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
随机推荐
- [译]Memory Reordering Caught in the Act
原文:http://preshing.com/20120515/memory-reordering-caught-in-the-act/ 编写lock-free的C/C++程序时,在保证memory ...
- WIN8+VS2013编写发布WCF之二(部署)
上文简介了如何建立WCF工程并且调试,下面说一下如何部署. 本文将陆陆续续讲述三种部署方式,随着项目的进展将不断补全. 声明: 用管理员身份打开VS2013,发布前请将程序的.net版本改成与服务器相 ...
- Vmware下Ubuntu无法上网的问题
本来这个挺简单的个问题,但是由于很久没有使用虚拟机并且期间实体机网络环境发生了一些变化,导致了一些麻烦. 一般用NAT就行了,就是Vmware右下角那个图标(左起第4个)设置就行. 我这么设置了还是不 ...
- char型变量理解
char c = 128; printf("%d", c); 问输出是多少? 正确答案应该是-128. 如下几种情况: char c=128;printf("%u\n& ...
- 微软TTS尝试系列之开篇杂谈(仅思路)
第一次写博客,不知道如何下手,思路也乱,就先聊聊怎么进的园子吧,但愿不会浪费大家太多的宝贵时间>_<. 与博客园结缘应该是大三刚开始的时候.当时学校教务处想开发一个教务安排系统,为了省钱就 ...
- 浅谈Objective—C中的面向对象特性
Objective-C世界中的面向对象程序设计 面向对象称程序设计可能是现在最常用的程序设计模式.如何开发实际的程序是存在两个派系的-- 面向对象语言--在过去的几十年中,很多的面向对象语言被发明出来 ...
- ref和out的区别
ref类型参数是按地址传递,能改变原来的数值.使用ref传参前,变量必须赋值. 带有ref类型参数的函数,不会清空变量,所以离开该函数的时候,所有ref引用的变量可以赋值也可以不赋值. out类型参数 ...
- 在Windows下使用BAT调度存储在资源库中的KTR
描述: 在Windows下使用BAT调度存储在资源库中的KTR 准备环境: 1.ktr文件(该KTR必须是存储在资源管库中的) 2.bat文件 @echo off D: cd D:\software\ ...
- linux系统设置-防火墙
基础知识 Linux系统内核内建了netfilter防火墙机制.Netfilter(数据包过滤机制),所谓的数据包过滤,就是分析进入主机的网络数据包,将数据包的头部数据提取出来进行分析,以决该连接为放 ...
- INFORMIX数据库常用命令
INFORMIX数据库常用命令 一.onstat命令集 1.onstat - 说明:查看数据库当前的状态 用法:onstat - 2.onstat -c 说明:查看数据库的配置文件 用法:ons ...