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. linux常用C函数目录

    字符测试篇 isalnum isalpha isascii iscntrl isdigit isgraphis islower isprint isspace ispunct isupper isxd ...

  2. [好文翻译]WEB前端性能优化的14条规则

    作为一个半前端工程师,而且只会写点HTML5和CSS3的“假”前端工程师,为了能更好地理解一下前端的花花世界,最近拜读了<高性能网站建设指南>一书,对作者提出的前端性能优化的14个规则获益 ...

  3. 上传文件multipart form-data boundary 说明

    含义 ENCTYPE="multipart/form-data" 说明: 通过 http 协议上传文件 rfc1867协议概述,客户端发送内容构造. 概述              ...

  4. PHPnow For ASP&&ASP.NET&&MongoDB&&MySQL支持VC6.0编译器&&MySQL升级

    可能和大家熟悉的是LAMP,Linux+Apache+Mysql+PHP,在Windows上,可能大家比较熟悉的是WAMP,Windows+Apache+Mysql+PHP,这是一个集成环境,说到集成 ...

  5. 【SpringCloud】Netflix源码解析之Ribbon:负载均衡策略的定义和实现

    Ribbon负载均衡策略定义 IRule其实就只做了一件事情Server choose(Object key),可以看到这个功能是在LB中定义(要求)的,LB把这个功能委托给IRule来实现.不同的I ...

  6. 如何在Linux中发现IP地址冲突

    导读 你们都知道什么是IP地址,是吧?它们被分配给网络上的设备来代表它们.它们通过DHCP服务器分配并且会经常改变.现在有两种IP地址.动态的一种会经常改变(几天一次),而静态的就如它的名字那样是静态 ...

  7. [Node.js]26. Level 5 : Route rendering

    Instead of just writing out the quote to the response, instead render the quote.ejs template, passin ...

  8. Java程序调用带参数的shell脚本返回值

    Java程序调用带参数的shell脚本返回值 首先来看看linux中shell变量(\(#,\)@,$0,$1,\(2)的含义解释 变量说明: -  \)$  Shell本身的PID(ProcessI ...

  9. _PyUnicodeUCS4_AsDefaultEncodedString

    "_PyUnicodeUCS4_AsDefaultEncodedString"的出现一般源于你在两个地方分别装了python,两个分别用UCS2和UCS4编码进行的build. 解 ...

  10. Modifying a Request or Response

    To make custom changes to web requests and responses, use FiddlerScript to add rules to Fiddler's On ...