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. sql server2005 express和Northwind数据库安装

    最近在学<C#入门经典>的数据库章节时,发现机子上既没有sql server又没书中所说的northwind数据库,想立刻运行下第一个工程DataReading都没法进行.在网上折腾后有了 ...

  3. Minimum Window Substring leetcode java

    题目: Given a string S and a string T, find the minimum window in S which will contain all the charact ...

  4. Log 日志工具类 保存到文件 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  5. python中 对文件的读写操作 以及如何边写入 边保存flush()

    转自:https://blog.csdn.net/t8116189520/article/details/78854708 首先 python中打开文件大致常用的几类如下: 1.写入文件write # ...

  6. Typescript declaration: Merge a class and an interface

    参考: https://stackoverflow.com/questions/47670959/typescript-declaration-merge-a-class-and-an-interfa ...

  7. Iterable转List

    Iterable转List Iterable<Entity> geted = entityDao.findAll(); List<Entity> list = Lists.ne ...

  8. php中对MYSQL操作之预处理技术(1)数据库dml操作语句

    <?php //预处理技术 //创建一个mysqli对象 $mysqli = new MySQLi("主机名","mysqlusername"." ...

  9. C++中用完需要释放掉内存的几个类

      BSTR BSTR bstrXML = NULL; //用完以后,或者 catch段中 if(bstrXML) ::SysFreeString(result); VARIANT VARIANT v ...

  10. C++:cin、cin.getline()、getline()的用法

    主要内容: 1.cin用法 2.cin.getline()用法 3.getline()用法 3.注意的问题 一.cin>> 用法1:输入一个数字或字符 #include <iostr ...