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]的更多相关文章

  1. LRU Cache leetcode java

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  2. [LeetCode] 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之LRU Cache 最近最少使用算法 缓存设计

    设计并实现最近最久未使用(Least Recently Used)缓存. 题目描述: Design and implement a data structure for Least Recently ...

  5. 【LeetCode OJ】LRU Cache

    Problem Link: http://oj.leetcode.com/problems/lru-cache/ Long long ago, I had a post for implementin ...

  6. leetcode@ [146] LRU Cache (TreeMap)

    https://leetcode.com/problems/lru-cache/ Design and implement a data structure for Least Recently Us ...

  7. [LeetCode]LRU Cache有个问题,求大神解答【已解决】

    题目: Design and implement a data structure for Least Recently Used (LRU) cache. It should support the ...

  8. 【LeetCode】LRU Cache 解决报告

    插话:只写了几个连续的博客,博客排名不再是实际"远在千里之外"该.我们已经进入2一万内. 再接再厉.油! Design and implement a data structure ...

  9. [LeetCode] 146. LRU Cache 最近最少使用页面置换缓存器

    Design and implement a data structure for Least Recently Used (LRU) cache. It should support the fol ...

随机推荐

  1. CentOS 7 搭建 LNMP

    一.安装httpd 1.yum install httpd -y 2.启动服务:systemctl start httpd 3.设置开机启动:systemctl enable 二.安装mariadb ...

  2. JS获取当前时间并把时间赋给input表单

    <script>window.onload = function(){ function getDate(){ debugger; var today = new Date(); var ...

  3. js弹出对话框的方法总结

    九种js弹出对话框的方法总结,需要的朋友可以参考一下 [1.最基本的js弹出对话框窗口代码] 这是最基本的js弹出对话框,其实代码就几句非常简单: <script LANGUAGE=" ...

  4. 从头开始一步一步实现EF6+Autofac+MVC5+Bootstarp极简前后台ajax表格展示及分页(二)前端修改、添加表格行点击弹出模态框

    在前一篇中,由于不懂jquery,前端做的太差了,今天做稍做修改,增加一个跳转到指定页面功能,表格行点击样式变化.并且在表格中加入bootstarp的按钮组,按钮点击后弹出模态框,须修改common, ...

  5. convert \uXXXX String to Unicode Characters in Python3.x

    转换\uXXXX if Python3.x: str.decode no longer exists in 3.x. that']s why Python 3.4: str : AttributeEr ...

  6. Android实现归属地查询功能

    实现归属地查询大体有两种方法可以实现,一种是通过归属地API进行查询,另一种是查询本地数据库.两种方法各有优劣,最好结合起来使用,我本次采用的是查询数据库的方法.首先需要从网上下载归属地数据库call ...

  7. win7下JDK环境变量设置方法

    来源于:http://jingyan.baidu.com/article/e9fb46e1b2b3347521f766c1.html

  8. (转)将cocos2dx项目从VS移植到Eclipse

    本文转自:http://www.cnblogs.com/Z-XML/p/3349518.html 引言:我们使用cocos2d-x引擎制作了一款飞行射击游戏,其中创新性地融入了手势识别功能.但是我 们 ...

  9. 2016年12月20日 星期二 --出埃及记 Exodus 21:15

    2016年12月20日 星期二 --出埃及记 Exodus 21:15 "Anyone who attacks his father or his mother must be put to ...

  10. JAVA-Semaphore信号灯-可实现维护自身线程访问数

    import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Ex ...