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 ...
随机推荐
- Python 函数 -xrange()
xrange() xrange() 函数用法与 range 完全相同,所不同的是生成的不是一个数组,而是一个生成器. 语法: xrange(stop) xrange(start, stop[, ste ...
- F4NNIU 版本的标准电阻列表(2018-09-29 更新)
F4NNIU 版本的标准电阻列表(2018-09-29 更新) 值代码 电阻值 格式化值 单位 公差代码 公差 格式化值数字 描述 0RJ 0 0 R J 5% J0000 0R 5% (0RJ) 1 ...
- 原生 Javascript 编写五子棋
原文地址:原生 Javascript 编写五子棋 博客地址:http://www.extlight.com 一.背景 近一个月没写 Javascript 代码,有点生疏.正好浏览网页时弹出五子棋的游戏 ...
- 最近项目和java对接,涉及到java的DESede加解密算法
google后找到这个作者的一篇博客,搬过来用 http://hersface.com/page/17.html <?php class DESede{ /** * 加密 * @param $d ...
- 页面初始加载的是默认刷新一次(f5)
参考找不到了,不好意思.. 两种可以都试一下,解决问题就好了. 1.----------- <script type="text/javascript"> window ...
- ARP的一次请求与应答
ARP: 我们知道,网络层和网络层以上使用的是IP地址,但在实际网络的链路上传送数据帧时,数据包首先是被网卡接受到再去处理上层协议的,所以最终还是必须使用该网络的硬件地址.但IP地址和下面的网络的硬件 ...
- mysql的约束
SQL 约束 约束用于限制加入表的数据的类型. 可以在创建表时规定约束(通过 CREATE TABLE 语句),或者在表创建之后也可以(通过 ALTER TABLE 语句). (1)NOT NULL约 ...
- 第七章 HTTP流量管理(二) URL 重写
URL 重定向功能: 浏览器中输入 http://<host_name>:31380/v1/service-A/XXXX 经过下面的重定向,实际调用的是serviceA的/v1/XXXX ...
- Apache中按天分割日志(Windows)
网上很多资料都有对Apache的access.log按天生成的方法,但在Windows server下稍有不同: 1.打开httpd.conf配置文件找到: CustomLog "logs/ ...
- window下boost库
1.下载boost开发库源码. 2.使用vs2008的命令行工具,进入到源码目录xxx/boost_1_58_0,命令行中运行bootstrap.bat,生成文件b2.exe,在命令行中执行b2.ex ...