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 ...
随机推荐
- 利用tween,使用原生js实现模块回弹动画效果
最近有一个需求,就是当屏幕往下一定像素时,下方会有一个隐藏的模块马上显现出来,向上运动后带有回弹效果.然后屏幕滚回去时这个模块能够原路返回 其实这个效果css3就可以很轻松实现,但是公司要求最低兼容i ...
- 一个iOS项目中包含多个xcodeproj文件,如何运行其中的一个项目
从GitHub上下载的Masonry的iOS源码,打开发现有多个项目,直接运行,模拟器没反应.由于Masonry是一个多工程的项目,每个项目都依赖Masonry的源码,所以要运行Masonry的Exa ...
- RDIFramework.NET ━ 9.3 用户管理 ━ Web部分
RDIFramework.NET ━ .NET快速信息化系统开发框架 9.3 用户管理 -Web部分 用户管理模块主要是对可登录系统的用户进行管理.后续的工作如:用户归属角色.权限的分配.用户所拥有 ...
- Linux安装vim失败的解决办法
最近想了解一下linux编程,于是linux系统下输入vim,发现竟然没有安装.好吧,那就安装吧.命令: sudo apt-get install vim 百度百科:apt-get是一条linux命令 ...
- StructureMap使用方法(转)
终于到了题目中的MVC使用StructureMap依赖注入的配置与实现了.在ASP.Net三层架构应用中StructureMap的是最老的IOC/DI工具,也就是依赖注入,很多线上的项目都使用了Str ...
- .Net程序员安卓学习之路4:使用xutils Get Post数据
前面使用了一些网络上找来的类进行网络访问,后来发现了安卓开发中有一个国人写的类库xutils比较全面,也比较经典,故后续使用xutils类库进行记录. 本例服务端使用WCF来实现,写好的WCF服务端在 ...
- PHPwind高级伪静态规则及方法
phpwind iis下伪静态规则[ISAPI_Rewrite]RewriteRule ^(.*)/(.*)-htm-(.*)-(.*)\.html$ $1/$2\.php\?$3=$4Rewrite ...
- CentOS 更改yum源与更新系统
FROM:http://www.cnblogs.com/lightnear/archive/2012/10/03/2710952.html [1] 首先备份/etc/yum.repos.d/CentO ...
- RegExp类型,单体内置对象
1.RegExp类型: 1)什么是正则表达式:RegExp --是一种规则,模式 --强大的字符串匹配工具 --创建: --使用RegExp构造函数创建:var re=new ...
- HDU 5686:2016"百度之星" - 资格赛 Problem B
原文链接:https://www.dreamwings.cn/hdu5686/2645.html Problem B Time Limit: 2000/1000 MS (Java/Others) ...