146. LRU Cache (List, HashTable)
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)的更多相关文章
- leetcode 146. LRU Cache 、460. LFU Cache
LRU算法是首先淘汰最长时间未被使用的页面,而LFU是先淘汰一定时间内被访问次数最少的页面,如果存在使用频度相同的多个项目,则移除最近最少使用(Least Recently Used)的项目. LFU ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- Java for LeetCode 146 LRU Cache 【HARD】
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- leetcode 146. LRU Cache ----- java
esign and implement a data structure for Least Recently Used (LRU) cache. It should support the foll ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- 146. LRU Cache
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 【LeetCode】146. LRU Cache
LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should suppo ...
- [LeetCode] 146. LRU Cache 近期最少使用缓存
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
- [LC] 146. LRU Cache
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- 好用的 convert freestyle jenkins jobs to pipeline 插件使用
1. 安装 安装,重启即可,非常简单 2. 使用 原有配置,主要列出执行 生成的jenkins pipeline 3. 构建效果 4. 参考资料 https:// ...
- minio 安装以及使用
1. 为了方便使用 docker 安装 docker run -p 9000:9000 minio/minio server /export 注意启动显示的 appkey secretkey C ...
- php查询mysql数据库 查询条件替中文字符串变量时无法查询
$temp2 ='十年';mysql_query("SET NAMES GBK"); $res = mysql_query("select songer_name fro ...
- (转)Android 读取联系人(详细)
import java.io.InputStream; import org.json.JSONArray; import org.json.JSONException; import org.jso ...
- (三)、Fiddler的使用
Fiddler的基本使用 1.先来看看Fiddler的基本界面 会话列表中各项的意思: Result: 表示HTTP返回的状态码 Protocal: 请求的协议,如:HTTP.HTTPS Host: ...
- 关于CSS单位:rem vh vw vmin vmax
rem(root em) 如果你给body设置了font-size字体大小,那么body的任何子元素的1em就是等于body设置的font-size demo: body { font-size: ...
- ORACLE用户的加锁、解锁
在DBA的日常工作中,经常遇到为Oracle用户解锁的操作:这篇文章给出在命令行下进行Oracle用户解锁的操作方法,通过几条简单的解锁语句就能完成此项工作.下面是具体的过程: 默认的scott用户是 ...
- Oracle拆分字符串函数与执行调用
本函数可以将“目标字符串”以“指定字符串”进行拆分,并通过表结构返回结果.代码如下: ); CREATE OR REPLACE FUNCTION splitstr(p_string IN VARCHA ...
- GOF23设计模式之解释器模式(interpreter)
一.解释器模式概述 (1)它是一种不常用的设计模式: (2)由于描述如何构成一个简单的语言解释器,主要用于使用面向对象语言开发的编译器和解释器的设计: (3)当我们需要开发一种新的语言时,可以考虑使用 ...
- ARP的一次请求与应答
ARP: 我们知道,网络层和网络层以上使用的是IP地址,但在实际网络的链路上传送数据帧时,数据包首先是被网卡接受到再去处理上层协议的,所以最终还是必须使用该网络的硬件地址.但IP地址和下面的网络的硬件 ...