LRU Cache

Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set.

get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
set(key, value) - Set or insert the value if the key is not already present. When the cache reached its capacity, it should invalidate the least recently used item before inserting a new item.

这题关键在于,怎样判断每个value是否算“最近使用”?

一个简单的想法是对每个键值对保留一个年龄,当cache满时,删除最“老”的键值对。

然而在删除节点时,寻找最“老”节点需要O(n)时间。

因此建立一个双向链表,最近使用的调整到头部,需要删除则删除尾部。

这样寻找最“老”节点就为O(1)时间。

然而在get函数时查找所需节点仍为O(n)时间。

因此再加入映射表m,空间换时间,查找变为O(1)。

struct Node
{
int key;
int val;
Node* prev;
Node* next;
Node(int k, int v): key(k), val(v), prev(NULL), next(NULL) {}
}; class LRUCache{
public:
Node* head; //most recently used
Node* tail; //least recently used
unordered_map<int, Node*> m;
int curcap;
int maxcap; LRUCache(int capacity) {
head = new Node(-, -);
tail = new Node(-, -);
head->next = tail;
tail->prev = head;
curcap = ;
maxcap = capacity;
} int get(int key) {
if(m[key] == NULL)
return -;
else
{
Node* node = m[key];
delnode(node);
addtohead(node);
return node->val;
}
} void set(int key, int value) {
if(m[key] == NULL)
{
if(curcap == maxcap)
{
m[tail->prev->key] = NULL;
delnode(tail->prev);
}
Node* node = new Node(key, value);
addtohead(node);
m[key] = node;
if(curcap < maxcap)
curcap ++;
}
else
{
Node* node = m[key];
node->val = value;
delnode(node);
addtohead(node);
}
} void delnode(Node* node)
{
Node* prev = node->prev;
Node* next = node->next;
prev->next = next;
next->prev = prev;
} void addtohead(Node* node)
{
node->next = head->next;
node->prev = head;
head->next->prev = node;
head->next = node;
}
};

【LeetCode】146. LRU Cache的更多相关文章

  1. 【LeetCode】146. LRU Cache 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典+双向链表 日期 题目地址:https://le ...

  2. leetcode 146. LRU Cache 、460. LFU Cache

    LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...

  3. LeetCode题解: LRU Cache 缓存设计

    LeetCode题解: LRU Cache 缓存设计 2014年12月10日 08:54:16 邴越 阅读数 1101更多 分类专栏: LeetCode   版权声明:本文为博主原创文章,遵循CC 4 ...

  4. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  5. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  6. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  7. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

随机推荐

  1. 内存控制篇calloc free getpagesize malloc mmap munmap

    calloc(配置内存空间) 相关函数 malloc,free,realloc,brk 表头文件 #include <stdlib.h> 定义函数 void *calloc(size_t ...

  2. 深度学习数据集Deep Learning Datasets

    Datasets These datasets can be used for benchmarking deep learning algorithms: Symbolic Music Datase ...

  3. 【tyvj五月有奖赛 暨Loi 55 Round #1】

    解题报告: 傻逼错误天天犯QAQ 第一题:简单DP,f[i][j]表示第 i 道题选 j 的最大得分,可以从f[i-1][j-1],f[i-1][j],f[i-1][j+1]转移过来,其实是可以滚动数 ...

  4. Android几种Service常驻内存的小思路

    老话说的好:躲得了初一,躲只是高三 ! 大多数的Android开发人员遇到的一个问题-怎样保证Service常驻内存. 近期我最终也在项目中务必幸运的遇到了 先来了解一下什么是Service常驻内存. ...

  5. C++ 使用TinyXML解析XML文件

    1.介绍 读取和设置xml配置文件是最常用的操作,TinyXML是一个开源的解析XML的C++解析库,能够在Windows或Linux中编译.这个解析库的模型通过解析XML文件,然后在内存中生成DOM ...

  6. nginx 域名绑定 域名, nginx 域名绑定 端口

    一.nginx 域名绑定 域名 nginx绑定多个域名可又把多个域名规则写一个配置文件里,也可又分别建立多个域名配置文件,我一般为了管理方便,每个域名建一个文件,有些同类域名也可又写在一个总的配置文件 ...

  7. Android的硬件抽象层模块编写规范

    硬件抽象层模块编写规范 ​ Android系统的硬件抽象层以模块的形式来管理各个硬件訪问接口.每个硬件模块都相应有一个动态链接库文件.这些动态链接库文件的命令须要符合一定的规范.同一时候,在系统内部. ...

  8. 敏捷开发方法XP的12个最佳实践

    极限编程(eXtreme Programming,简称XP)是一种轻量级.高效.低风险.柔性.可预测的.科学的软件开发方法,其特性包含在12个最佳实践中. 1.  计划游戏 ( Planning Ga ...

  9. linux用户管理中两个重要的“父子”配置文件

    在Linux中主要通过用户配置文件来查看和修改用户信息,因此下面我们将介绍两个重要的用户配置文件,让你能够更好的hold住你的用户. 一:父文件/etc/passwd 1.查看配置文件/etc/pas ...

  10. Java HashMap 默认排序

    先看一段Java代码. package com.m58.test; import java.text.ParseException; import java.text.SimpleDateFormat ...