LeetCode OJ-- LRU Cache ***@
https://oj.leetcode.com/problems/lru-cache/
涨姿势的一道题,相当于实现一种数据结构,实现最近最少使用数据结构。
// 用来记录 前后节点都是什么 类似链表上的节点
struct DataNode{
int key, value;
DataNode *prev, *next;
DataNode(int _key, int _value){
key = _key;
value = _value;
prev = NULL;
next = NULL;
}
};
class LRUCache{
public:
int capacity;
DataNode *head;
DataNode *tail;
//用来快速查找,key在不在。本来用stack最自然了,但是它不支持中间位置的删除元素操作
map<int,DataNode*> cache; LRUCache(int capacity) {
this->capacity = capacity;
head = NULL;
tail = NULL;
}
void moveToHead(DataNode *p)
{
if(p == head) // p is head
return;
if(p == tail)
{
tail = p->prev;
tail->next = NULL; p->prev = NULL;
p->next = head;
head->prev = p;
head = p;
}
else
{
p->prev->next = p->next;
p->next->prev = p->prev; p->prev = NULL;
p->next = head;
head->prev = p;
head = p;
}
}
//取得值,并把这个值的node挪到最前面,相当于使用了一次了
int get(int key) {
if(cache.count(key) == )
return -;
DataNode *p = cache[key];
moveToHead(p);
return p->value;
}
//如果不存在,则插入到最前面。如果存在则设置值,并把这个值的node挪到最前面,相当于使用了一次了
void set(int key, int value) {
if(cache.count(key) == ) //本来不包含
{
DataNode *p = new DataNode(key,value);
cache.insert(make_pair(key,p)); if(head == NULL)
{
head = p;
tail = p;
}
else // p is new head
{
p->prev = NULL;
p->next = head;
head->prev = p;
head = p;
}
// remove tail
if(cache.size() > capacity)
{
cache.erase(tail->key);
tail = tail->prev;
tail->next = NULL;
}
}
else
{
DataNode *p = cache[key];
p->value = value;
moveToHead(p);
}
}
};
LeetCode OJ-- LRU Cache ***@的更多相关文章
- 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之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- 【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
- [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 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【leetcode】LRU Cache
题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...
- 【leetcode】LRU Cache(hard)★
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- 【Leetcode】 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 ...
随机推荐
- 线程之sleep(),wait(),yield(),join()等等的方法的区别
操作线程的常用方法大体上有sleep(),join(),yield()(让位),wait(),notify(),notifyAll(),关键字synchronized等等. 由于这些方法功能有些 ...
- UVA_1025 a Spy in the Metro 有向无环图的动态规划问题
应当认为,有向无环图上的动态规划问题是动态规划的基本模型之一,对于某个模型,如果可以转换为某一有向无环图的最长.最短路径问题,则可以套用动态规划若干方法解决. 原题参见刘汝佳紫薯267页. 在这个题目 ...
- P1194 买礼物(建模)
P1194 买礼物 题目描述 又到了一年一度的明明生日了,明明想要买B样东西,巧的是,这B样东西价格都是A元. 但是,商店老板说最近有促销活动,也就是: 如果你买了第I样东西,再买第J样,那么就可以只 ...
- PHP的抽象类、接口的区别和选择
1.对接口的使用是通过关键字implements.对抽象类的使用是通过关键字extends.当然接口也可以通过关键字extends继承. 2.接口中不可以声明成员变量(包括类静态变量),但是可以声明类 ...
- laravel5.2总结--blade模板
## 1.基本用法 ``` ##情形1 $name = laravel5 <div class="title"> {{$name}} {{$name}}</div ...
- 简单实现nodejs爬虫工具
约30行代码实现一个简单nodejs爬虫工具,定时抓取网页数据. 使用npm模块 request---简单http请求客户端.(轻量级) fs---nodejs文件模块. index.js var ...
- Android 使用intent传递返回值:startActivityForResult()与onActivityResult()与setResult()参数分析,activity带参数的返回
在一个父Activity通过intent跳转至多个不同子Activity上去,当子模块的代码执行完毕后再次返回父页面,将子activity中得到的数据显示在主界面/完成的数据交给父Activity处理 ...
- vue tradingView(二)
tradingView 一些配置问题 tradingView 一些配置问题 javascript Demo_Hu 4月17日提问 · 4月17日更新 9 关注 1 收藏,993 浏览 问题对人有帮助, ...
- 从xml文件中绑定数据到DropDownList控件上
参考了2篇文章: http://www.cnblogs.com/JuneZhang/archive/2010/11/23/1885671.html http://blog.sina.com.cn/s/ ...
- [python][django 1.10中文文档]
https://docs.djangoproject.com/en/1.10/ 官方文档,点我下载 推荐一个翻译django 1.8.2的网址: 推荐一个翻译django 1.10的博客:(着重推荐 ...