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. 04vim的使用

    linux常用命令 workon 查看已经安装的虚拟环境 deactivate 退出虚拟环境 whoami 查看用户 sudo bash install.sh 添加权限 pwd 查看在那个路径下 cd ...

  2. 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8

    a,b,c,d,e,f,g=1,2,3,4,5,8,9 m = a > b and c < d or c > e n = b > a or g < f x = m and ...

  3. Python中的tuple

    tuple_lst = [ ('元祖容器可哈希',), ('元祖中的元素不可直接修改',), ('元祖可迭代',), ('查',), ('练习',), ] 元祖容器可哈希 >>>ha ...

  4. Codeforces Round #462 (Div. 2) A Compatible Pair

    A. A Compatible Pair time limit per test1 second memory limit per test256 megabytes Problem Descript ...

  5. 动态规划:HDU1069-Monkey and Banana

    Monkey and Banana Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. Hive UDTF开发指南

    在这篇文章中,我们将深入了解用户定义表函数(UDTF),该函数的实现是通过继承org.apache.Hadoop.hive.ql.udf.generic.GenericUDTF这个抽象通用类,UDTF ...

  7. datagrid的toolbar的两种实现方式

    datagrid的toolbar的两种实现方式 1.在html文件中,可以在设置toolbar="#tb",再在div中设置id="tb" <table ...

  8. IOS开发---菜鸟学习之路--(十六)-将Image转换为Base64

    我们直接在.m文件的引用头文件部分 和 @interface   AddPictureViewController () 之间  加入 增加部分的代码 然后就可以使用图片转Base64了 #impor ...

  9. 3、CSS基础 part-1

    1.给body设置颜色 <html> <body text="red"> <p> hello world</p> <p> ...

  10. WINDOWS批量替换不同文件夹下的相同文件

    今天帮媳妇解决的问题,记录一下,也许以后有用 例子: N个文件夹下有同一个文件(common.php),但是,现在对common.php文件进行了大量修改. 现在想用最新的common.php替换掉所 ...