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. Linux 错误码对照表

    errno 在 <errno.h> 中定义,错误 Exx 的宏定义在 /usr/include/asm-generic 文件夹下面的  errno-base.h 和 errno.h,分别定 ...

  2. Unite 2018 | 《崩坏3》:在Unity中实现高品质的卡通渲染(下)

    http://forum.china.unity3d.com/thread-32273-1-1.html 今天我们继续分享米哈游技术总监贺甲在Unite Beijing 2018大会上的演讲<在 ...

  3. JS和C#对Json的操作

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式.博主记得几年前在华为外包项目中有一个和Android应用交互的需求,Andr ...

  4. quick code ide设置分辨率

    默认是640*960的分辨率,但是我的设计分辨率是480*800 所以得改改 在config.lua里的改成 -- design resolutionCONFIG_SCREEN_WIDTH = 480 ...

  5. redis+php实现微博功能(一)

    (一).微博功能概况 微博用户账号注册 微博用户登录 微博发布 添加微博好友(粉丝) 微博推送 微博冷数据写入mysql数据库 (二).redis数据结构设计 这节分享微博用户注册与登录:我们完全采用 ...

  6. Python学习总结之一 -- 基础篇

    Python学习第一篇 一:写在前面 啊,最近我的新博客一直都没有更新学习内容了,只是最近一直都在忙着寻找实习机会(或许这只是一个借口,真实原因是我太懒惰了,改改改!).终于今天又投递了几个新的实习职 ...

  7. Data_Structure01-绪论作业

    一.作业题目 仿照三元组或复数的抽象数据类型写出有理数抽象数据类型的描述 (有理数是其分子.分母均为整数且分母不为零的分数). 有理数基本运算: 构造有理数T,元素e1,e2分别被赋以分子.分母值 销 ...

  8. cpp分解质因数

    原理有点像埃氏筛. #include <stdio.h> #include <iostream> #include <stdlib.h> using namespa ...

  9. GOF23设计模式之桥接模式(bridge)

    一.桥接模式概述 桥接模式核心要点: 处理多层继承结构,处理多维度变化的场景,将各个维度设计成独立的继承结构,使各个维度可以独立的扩展在抽象层建立关联. 二.桥接模式场景提出与存在问题 商城系统中常见 ...

  10. 数据结构与算法JavaScript描述——栈

    栈就是和列表类似的一种数据结构,它可用来解决计算机世界里的很多问题. 栈是一种高效的数据结构,因为数据只能在栈顶添加或删除,所以这样的操作很快,而且容易实现. 栈的使用遍布程序语言实现的方方面面,从表 ...