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. dp专练

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

  2. Android stadio litepal

    今天看到技术交流群里有人招聘Android,要求会litepal. 我立马百度了下.嗯,我的学习技术的精神,是值得称赞的. litepal就是操作数据库的一个框架.git地址: https://git ...

  3. 洛谷P1079 Vigenère 密码

    题目链接:https://www.luogu.org/problemnew/show/P1079

  4. Django 六——自定义标签、图片验证码、发送邮件、评论树、组合搜索

    1.自定义标签 2.图片验证码 3.生成邮箱验证码.发送邮件 4.评论树实现 5.组合搜索(Q) 1.自定义标签 配置: a.在app中新建文件夹  templatetags,里面新建  xx.py文 ...

  5. Python框架之Django学习笔记(三)

    开始一个项目 第一次使用 Django,必须进行一些初始化设置工作. 新建一个工作目录,例如 D:\tool\python\Python27\workspace\djcode,然后进入该目录. 转到创 ...

  6. Beamer模板

    普通模板: \documentclass[UTF-8]{beamer} \usepackage{ctex} \usetheme{CambridgeUS} \begin{document} \secti ...

  7. 【LoadRunner】对摘要认证的处理

    近期项目中,进行http协议的接口性能测试过程中,需要进行登录接口的摘要认证,分享一下测试经验. 测试准备 测试工具:LoadRunner11 测试类型:接口测试--某系统登录接口 步骤 根据系统接口 ...

  8. Python-S9——Day100-Web前端框架之Vue

    01 课程简介: 02 let和const: 03 箭头函数: 04 对象的单体模式: 05 nodejs介绍和npm操作: 06 webpack.babel介绍和vue的第一个案例: 07 昨日内容 ...

  9. 使用 htaccess 重写 url,隐藏查询字符串

    例如我们有如下 URL: http://example.com/users.php?name=tania 但是我们想要让 URL 变成如下: http://example.com/users/tani ...

  10. Spring框架DataSource

    一 DataSource 简易介绍 JDK里 javax.sql的一个接口 public interface DataSource 表示无力数据源的连接,作为DriverManager设施的替代项, ...