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. mac 升级vim

    首先,要下载vim的源代码.Vim source archives : vim online,下载7.4的新建一个目录用于安装vim 7.4:sudo mkdir /usr/local进入源代码的sr ...

  2. Struts2(五):ActionSupport

    我们在上一章节中的一个列子中使用到了一个标识跳转到登录页面的例子: 示例是这样写的: index.jsp: <br/> <a href="gotoLoginPage&quo ...

  3. c/c++ 指针理解(1)

    指针是一个变量,存放变量的地址

  4. cell长按出错

    错误的原因: *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reaso ...

  5. 解决qt程序的链接阶段出现 undefined reference 错误

    错误的原因是我使用到了 QT Widgets 模块中的东西,但是makefile的链接的参数中没有 widgets.其实官网上提到了这个: http://doc.qt.io/qt-5/qtwidget ...

  6. python日志模块

    许多应用程序中都会有日志模块,用于记录系统在运行过程中的一些关键信息,以便于对系 统的运行状况进行跟踪.在.NET平台中,有非常著名的第三方开源日志组件log4net,c++中,有人们熟悉的log4c ...

  7. 配置文件之SharedPreferences

    新建配置文件类 /** * Created by RongGuang * 应用程序配置信息 */ public class AppOption { public static final String ...

  8. c#委托、事件、Observer

    委托和事件在.NET Framework[1] 中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易. 中文名 委托 外文名 Delegate 编程语言 C# 作     ...

  9. android sdk manager 无法更新解决方法

    因为在开始->运行->cmd 中敲入 ping dl-ssl.google.com -t 始终ping不通 ,关闭cmd后 首先需要下载一个代理服务器下载地址 http://pan.bai ...

  10. Aspect Oriented Programming (AOP)

    切面”指的是那些在你写的代码中在项目的不同部分且有相同共性的东西.它可能是你代码中处理异常.记录方法调用.时间处理.重新执行一些方法等等的一些特殊方式.如果你没有使用任何面向切面编程的类库来做这些事情 ...