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. MongDB之各种修改操作

    接口IMongDaoUpdate: package com.net.test.mongdb.dao; import com.net.test.mongdb.entity.User; public in ...

  2. 指针的操作 p*++

    int x, y, *px = &x, *py = &y; y = *px + ; //表示把x的内容加5并赋给y,*px+5相当于(*px)+5 y = ++*px; //px的内容 ...

  3. 动态规划:HDU1160-FatMouse's Speed(记录动态规划状态转移过程)

    FatMouse's Speed Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  4. Kali 安装VMtools(最新)

    老方法安装的VMtools不能进行主宿切换,下面是kali最新版安装VMtools的方法 一.换国内源&更新源 参考 Kali 2017更新源 二.安装VMtools apt-get inst ...

  5. Sql日期时间格式转换(转 子夜.)

    sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...

  6. 自定义RadioGrop,支持添加包裹着的RadioButton

    控件类: package com.chinaCEB.cebView; import android.annotation.TargetApi; import android.content.Conte ...

  7. Redis实现之整数集合

    整数集合 整数集合(insert)是集合键的底层实现之一,当一个集合只包含整数值元素,并且这个集合的元素数量不多时,Redis就会使用整数集合作为集合键的底层实现.举个栗子,如果我们创建一个只包含五个 ...

  8. SVM python小样例

    SVM有很多种实现,但是本章只关注其中最流行的一种实现,即序列最小化(SMO)算法在此之后,我们将介绍如何使用一种称为核函数的方式将SVM扩展到更多的数据集上基于最大间隔的分割数据优点:泛化错误率低, ...

  9. laravel5.2总结--数据库操作

    1 配置信息 1.1配置目录: config/database.php 1.2配置多个数据库 //默认的数据库 'mysql' => [ 'driver' => 'mysql', 'hos ...

  10. 2、HTML基础总结 part-2

    1.表单一 <html> <body> <form> 姓名: <input type="text" name="name&quo ...