LRU Cache [LeetCode]
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.
Solution:
class LRUCache{
public:
vector<int> keys;
unordered_map<int, int> map;
int size = ;
LRUCache(int capacity) {
size = capacity;
}
void adjust(int key){
int idx = -;
for(int i = keys.size() - ; i >= ; i --)
if(keys[i] == key){
idx = i;
break;
}
if(idx == -)
return;
keys.erase(keys.begin() + idx);
keys.push_back(key);
}
int get(int key) {
if(map.find(key) == map.end()){
return -;
}else{
adjust(key);
return map[key];
}
}
void set(int key, int value) {
if(map.find(key) != map.end()){
map[key] = value;
adjust(key);
return;
}
if(keys.size() >= size ){
int key_to_erase = keys[];
keys.erase(keys.begin());
map.erase(key_to_erase);
}
keys.push_back(key);
map[key] = value;
}
};
LRU Cache [LeetCode]的更多相关文章
- LRU Cache leetcode java
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- [LeetCode] 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之LRU Cache 最近最少使用算法 缓存设计
设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...
- 【LeetCode OJ】LRU Cache
Problem Link: http://oj.leetcode.com/problems/lru-cache/ Long long ago, I had a post for implementin ...
- leetcode@ [146] LRU Cache (TreeMap)
https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...
- [LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...
- 【LeetCode】LRU Cache 解决报告
插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...
- [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...
随机推荐
- 解决编译错误: 非法字符: '\ufeff' 解决方案|错误: 需要class, interface或enum
http://www.cnblogs.com/oymx/p/5353235.html 可以 note++打开 在格式里 选择utf-8 无bom格式编码 即可
- Java基础之扩展GUI——高亮元素、上下文菜单、移动旋转元素、自定义颜色(Sketcher 10)
窗口应用程序. 本例在上一版的基础上实现了高亮元素.移动元素.上下文菜单.旋转元素.设置自定义颜色. 1.自定义常量包: // Defines application wide constants p ...
- vue.js入门(3)——组件通信
5.2 组件通信 尽管子组件可以用this.$parent访问它的父组件及其父链上任意的实例,不过子组件应当避免直接依赖父组件的数据,尽量显式地使用 props 传递数据.另外,在子组件中修改父组件的 ...
- 简明python教程 --C++程序员的视角(一):数值类型、字符串、运算符和控制流
最初的步骤 Python是大小写敏感的 任何在#符号右面的内容都是注释 >>> help('print')在“print”上使用引号,那样Python就可以理解我是希望获取关于“pr ...
- Matlab的XTickLabel中数值带下标
%axis为'x'或'y',分别表示更改x或y刻度 %ticks是字符cell function settick(axis,ticks) n=length(ticks); tkx=get(gca,'X ...
- MVC中的JS和CSS压缩
小说一下Js和CSS压缩的好处: 1.减小了文件的体积 2.减小了网络传输量和带宽占用 3.减小了服务器的处理的压力 4.提高了页面的渲染显示的速度 很多建议将站点的静态文件(如图片.js.css ...
- zjuoj 3602 Count the Trees
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3602 Count the Trees Time Limit: 2 Seco ...
- ACM Coder [T1002] 一直wrong answer,不知道为什么。上代码!就对就对!
忘了改了什么,后来居然对了!做打不死的菜鸟! #include <stdio.h> #include <stdbool.h> #define arrayLength 20 #d ...
- jQuery对select操作小结
//遍历option和添加.移除optionfunction changeShipMethod(shipping){ var len = $("select[@name=ISHIPTYPE] ...
- [python]用Python进行SQLite数据库操作
用Python进行SQLite数据库操作 1.导入Python SQLITE数据库模块 Python2.5之后,内置了SQLite3,成为了内置模块,这给我们省了安装的功夫,只需导入即可~ ]: u ...