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 ***@的更多相关文章

  1. Java for LeetCode 146 LRU Cache 【HARD】

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

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

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

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

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

  4. 【LeetCode】LRU Cache 解决报告

    插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...

  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 近期最少使用缓存

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

  7. 【leetcode】LRU Cache

    题目简述: Design and implement a data structure for Least Recently Used (LRU) cache. It should support t ...

  8. 【leetcode】LRU Cache(hard)★

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

  9. 【Leetcode】 LRU Cache实现

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

  10. leetcode 146. LRU Cache ----- java

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

随机推荐

  1. STM32启动文件:startup_stm32f10x_hd.s等启动文件的简单描述

    在官方的库文件中,分别有如下文件: startup │ │ │ ├─arm │ │ │ │ startup_stm32f10x_cl.s │ │ │ │ startup_stm32f10x_hd.s ...

  2. Git add命令

    git add -A和 git add .   git add -u在功能上看似很相近,但还是存在一点差别 git add . :他会监控工作区的状态树,使用它会把工作时的所有变化提交到暂存区,包括文 ...

  3. vba中ListBox控件的使用

    给ListBox添加内容 If CheckBox8 = True Then---------------------------checkbox控件被选中 For i = 0 To ListBox1. ...

  4. dp专练

    dp练习. codevs 1048 石子归并 区间dp #include<cstdio> #include<algorithm> #include<cstring> ...

  5. Android学习笔记之-----讯飞语音识别实例化RecognizerDialog参数出现错误的解决方法

    本人也是个小菜鸟,在做语音识别时也遇到了这个问题,空指针一直报错,app程序停止运行. 在网上搜了半天在这个帖子里找到了解决方法:http://bbs.xfyun.cn/forum.php?mo .. ...

  6. loj2043 「CQOI2016」K 远点对

    k-d tree 裸题------ #include <algorithm> #include <iostream> #include <cstdio> using ...

  7. Wordpress 自定义文章类型添加 Categoried、Tags

    默认情况下 ,自定义文章类型没有分类和标签属性,需要通过 register_taxonomy_for_object_type 手动注册文章分类和标签,可以通过在 functions.php 或插件中添 ...

  8. Leetcode 503.下一个更大元素

    下一个更大元素 给定一个循环数组(最后一个元素的下一个元素是数组的第一个元素),输出每个元素的下一个更大元素.数字 x 的下一个更大的元素是按数组遍历顺序,这个数字之后的第一个比它更大的数,这意味着你 ...

  9. [oldboy-django][6其他]navicat远程登录没有权限

    day6-17-1204 # 增加远程访问mysql的权限(就是其他ip地址远程访问另外一个ip地址的数据库) -- step1 修改配置文件,bind_address, 允许所有ip地址都可以访问m ...

  10. 山贼集团 (group)

    山贼集团 (group) 题目描述 某山贼集团在绿荫村拥有强大的势力,整个绿荫村由N个连通的小村落组成,并且保证对于每两个小村落有且仅有一条简单路径相连.小村落用阿拉伯数字编号为1,2,3,4,-,n ...