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.

思路:Hashmap+double-linked list.

Hashmap: key是已给的key,value是ListNode的地址

double-linked list:当链表中某元素要被删除或提到head时,双向链表都方便进行操作

class LRUCache{
private:
/*double-linked list*/
struct ListNode{
ListNode * prev;
ListNode * next;
int key;
int value;
ListNode(int _key, int _value):key(_key), value(_value), prev(NULL), next(NULL){}
}; /*move the node to the head of liest*/
void change2TopPriority(ListNode* listNode){
if(head==listNode) return; //special case: it's the head
listNode->prev->next = listNode->next;
if(listNode->next) listNode->next->prev = listNode->prev;
else tail = tail->prev; //special case: it's the tail
listNode->prev = NULL;
listNode->next = head;
head->prev = listNode;
head = head->prev;
}
public:
LRUCache(int capacity) {
listNodeMap.clear();
this->capacity = capacity;
head = NULL;
tail = NULL;
} int get(int key) {
if(listNodeMap.find(key)!=listNodeMap.end()){
change2TopPriority(listNodeMap[key]);
return listNodeMap[key]->value;
}
else return -;
} void set(int key, int value) {
if(listNodeMap.find(key)==listNodeMap.end()){ // ket not exist
if(listNodeMap.size()==capacity){ // reach the capacity
//erase least used data
listNodeMap.erase(tail->key);
ListNode * tmp = tail;
if(head == tail) { //special case: capacity=1
head = tail = NULL;
}
else{
tail = tail->prev;
tail->next = NULL;
}
delete tmp;
}
//insert new node
ListNode * newNode = new ListNode(key,value);
if(!head){ //special case: first node in the list
head = tail = newNode;
}
else{
head->prev = newNode;
newNode->next = head;
head = head->prev;
}
listNodeMap[key]=newNode;
}
else{ //key already exists
listNodeMap[key]->value = value;
change2TopPriority(listNodeMap[key]);
}
}
private:
unordered_map<int,ListNode*> listNodeMap; //hashmap
ListNode* head; //head of double linked list
ListNode* tail; //tail of double linked list
int capacity; //capacity of CRU
};

146. LRU Cache (List, HashTable)的更多相关文章

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

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

  2. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  3. Java for LeetCode 146 LRU Cache 【HARD】

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  4. leetcode 146. LRU Cache ----- java

    esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...

  5. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  6. 146. LRU Cache

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  7. 【LeetCode】146. LRU Cache

    LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...

  8. [LeetCode] 146. LRU Cache 近期最少使用缓存

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

  9. [LC] 146. LRU Cache

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

随机推荐

  1. [MEF]第05篇 MEF的目录(Catalog)筛选

    一.演示概述本示例演示如何使用MEF提供的目录(Catalog)的扩展机制实现可过滤导出部件的自定义目录类.主要是通过继承ComposablePartCatalog基类,并实现接口INotifyCom ...

  2. Bootstrap组件福利篇:十二款好用的组件推荐

    阅读目录 一.时间组件 1.效果展示 2.源码说明 3.代码示例 二.自增器组件 1.效果展示 2.源码说明 3.代码示例 三.加载效果 一.实用型 二.炫酷型 四.流程图小插件 1.效果展示 2.源 ...

  3. server 2012系统更改电脑密码

    在server2012系统中将鼠标指针移至任务栏右侧,在弹出的操作栏中单击“设置”选项.   在打开的设置操作界面中,鼠标单击“控制面板”选项.     在打开的控制面板选项窗口中,单击“管理工具”选 ...

  4. win7怎么安装和启动 jboss

    本文以JBoss Application Server 4.2.1 GA(以下简称JBoss)为例,介绍它在Windows平台上的启动过程.为了方便叙述,对平台环境做以下假定:Java运行时的安装路径 ...

  5. SVN checkout 出的项目在PHPstorm中打开没有subversion(SVN)选项按钮怎么办?

    对于svn add 命令的批量操作,为了操作简便还是习惯在IDE中完成,有时候新checkout出的项目,在PHPstorm中右键菜单中没有 Subversion 按钮,操作如下: 点击VCS按钮,然 ...

  6. print 和 println的区别

    println 输出字符后,下一个输出的字符会换行展示 print 输出字符后,下一个输出字符不会会换展示

  7. 【洛谷】P1541 乌龟棋(四维背包dp)

    题目背景 小明过生日的时候,爸爸送给他一副乌龟棋当作礼物. 题目描述 乌龟棋的棋盘是一行N个格子,每个格子上一个分数(非负整数).棋盘第1格是唯一的起点,第N格是终点,游戏要求玩家控制一个乌龟棋子从起 ...

  8. JAVA构造函数在超类与子类定义鲁波总结

    1.子类无构造函数,超类无构造函数,创建的无参数的对象: 编译通过. class A { } class B extends A { } public class Testeeer { public ...

  9. 关于操作 ASP.NET Web API的实例

    WCF的野心造成了它的庞大复杂,HTTP的单纯造就了它的简单优美.为了实现分布式Web应用,我们不得不将两者凑合在一起 —— WCF服务以HTTP绑定宿主于IIS. 于是有了让人晕头转向的配置.让人郁 ...

  10. 04——wepy框架搭建

    wepy官方文档:https://tencent.github.io/wepy/document.html#/ 1.项目下载 # .安装wepy-cli npm install wepy-cli -g ...