[LeetCode]LRU Cache有个问题,求大神解答【已解决】
题目:
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.
这是我的代码:
1 class LRUCache{ 2 public: 3 int num; 4 int max; 5 list<int> latest_key; //用于保存使用情况,队头是最久未使用的,队尾是最近使用的 6 unordered_map<int, int> cache; //用于保存key,value 7 8 LRUCache(int capacity) { 9 num = 0; 10 max = capacity; 11 } 12 13 int get(int key) { 14 unordered_map<int, int>::iterator it = cache.find(key); 15 list<int>::iterator iter; 16 if (it == cache.end()) //如果没有找到key 17 return -1; 18 else //如果找到了key,就在对应的最近latest队列里面修改key的位置,先把key所在的位置删除,再把key放到队尾 19 { 20 iter = latest_key.begin(); 21 while (*iter != key) 22 iter++; 23 latest_key.erase(iter); 24 latest_key.push_back(key); 25 return it->second; 26 } 27 } 28 29 void set(int key, int value) { 30 unordered_map<int, int>::iterator it = cache.find(key); 31 list<int>::iterator iter; 32 if (it != cache.end()) //如果要插入的已经有key存在,就先在优先队列里面找到key出现的位置,删除,再把key插入队尾 33 { 34 it->second = value; 35 iter = latest_key.begin(); 36 while (*iter != key) 37 iter++; 38 latest_key.erase(iter); 39 latest_key.push_back(key); 40 } 41 else //如果要插入的不存在 42 { 43 if (num<max) //如果不超过cache容量,则直接在cahe中插入,再在队尾添加该key 44 { 45 num++; 46 cache.insert(std::pair< int, int >(key, value)); 47 latest_key.push_back(key); 48 } 49 else //如果cache已经满了,则根据队头元素,在cache删除对应键值,再在队列中删除这个队头,之后,把新要插入的键值插入cache中,把新key插入队尾 50 { 51 int latest = latest_key.front(); 52 cache.erase(latest); 53 latest_key.pop_front(); 54 cache.insert(std::pair< int, int >(key, value)); 55 latest_key.push_back(key); 56 } 57 } 58 } 59 };
当我把代码中出现:
1 iter = latest_key.begin(); 2 while (*iter != key) 3 iter++;
部分替换为:
1 iter=find(latest_key.begin(),latest_key.end(),key);
就会报错:
Time Limit Exceeded
Last executed input: 2048,[set(1178,3401),set(903,6060).....
我大致查了一下find的实现机制,也是遍历啊,按理说这两者效率差不多,为什么替换之后就不能通过?而替换之前能通过,求大神解答!!
万分感谢!!!
在Leetcode上问,已经得到答案:
之前的那个算法效率确实不高,压线过的,修改了原有代码,增加了一个unordered_map<int, list<int>iterator>用来索引list,可以使时间复杂度降到O(1):
1 class LRUCache{ 2 private: 3 unordered_map<int, int> cache; 4 unordered_map<int, list<int>::iterator> find_key; 5 list<int> latest_key; 6 int max; 7 public: 8 LRUCache(int capacity) : max(capacity){ 9 10 } 11 12 int get(int key) { 13 if (cache.find(key) == cache.end()){ 14 return -1; 15 } 16 latest_key.erase(find_key[key]); 17 latest_key.push_front(key); 18 find_key[key] = latest_key.begin(); 19 return cache[key]; 20 } 21 22 void set(int key, int value) { 23 if (cache.find(key) == cache.end()) { 24 if (cache.size() >= max) { 25 cache.erase(latest_key.back()); 26 latest_key.pop_back(); 27 cache[key] = value; 28 latest_key.push_front(key); 29 find_key[key] = latest_key.begin(); 30 } 31 else { 32 cache[key] = value; 33 latest_key.push_front(key); 34 find_key[key] = latest_key.begin(); 35 } 36 } 37 else { 38 cache[key] = value; 39 latest_key.erase(find_key[key]); 40 latest_key.push_front(key); 41 find_key[key] = latest_key.begin(); 42 } 43 } 44 };
转载于:https://www.cnblogs.com/yanqi0124/p/3806680.html
[LeetCode]LRU Cache有个问题,求大神解答【已解决】相关推荐
- 软件开发Linux环境下,java通过JNA调用so报错,求大神解答,感激不尽。
软件开发Linux环境下,java通过JNA调用so报错,求大神解答,感激不尽. 图片说明 最佳答案: 专家已采纳 先用c等调用一下so,看函数能否正确调用 文章来源:https://ask.csdn ...
- matlab 连续两个if,求大神解答一个matlab中的for循环嵌套if选择语句
公告: 为响应国家净网行动,部分内容已经删除,感谢读者理解. 话题:求大神解答一个matlab中的for循环嵌套if选择语句回答:1.最后缺少一个end;2.这句输出没什么意义:disp A(i)=A ...
- 一个限流电路,求大神解答
目前在做一个项目,是一个产品里面需要用一个螺线管来带动带动一个机械结构上锁或者解锁.螺线管直接接上12VDC的话会发热严重,所以决定设计一个电路进行降压限流,电路如下: 1. 12VDC输出时,流经螺 ...
- 计算机丢失first,求大神解答硬盘驱动丢失怎么办
某蛋的piglet桑 回答数:14533 | 被采纳数:5 2016-12-23 12:51:05 一.U盘重装系统 准备:一台正常开机的电脑和一个U盘 1.百度下载"U大师" ...
- java 网格包,求大神解答:JAVA网格包布局管理器小程序问题
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 为什么我在这个程序上加入了一个Container对象就出问题了,求解答,要如何改: package A; import java.awt.*; impor ...
- 求大神解答!!!!在线死等!
诉求:python能否实现在一个网站平台上输入账号密码后,能否开启多个同一账号下的标签页. 例如:如下图所示输入账号密码,打开网站后可以开启多个标签页,并且可以分别对每个标签页进行相同操作.
- 计算机游戏模式怎么开144hz,刚开始换成了144hz现在为何最高只有60hz了求大神解答...
电脑能够设置什么样的屏幕刷新率和分辨率不仅取决于显示器及其驱动程序,同时还取决于显卡及其驱动程序.也就是说,只有当显示器和显卡都支持某一种刷新率和分辨率模式时,操作系统才能设置出这种屏幕的刷新率和分辨 ...
- ExtJs选项卡,求大神解答
如图,我要在这个树下面加个选项卡,怎么弄 Ext.onReady(function() {//layout的north regionvar data = [{text: '一号摄像头',expande ...
- c语言未命名exe,用dev-c++编译出现问题,求大神解答啊
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 比你还菜,用了你的码,也是错误,编译日志里是这样的东西:编译器: Default compiler 执行 gcc.exe... gcc.exe " ...
最新文章
- java日历教程_JAVA Calendar方法使用基础教程详解
- linux用户管理常用命令
- 【C#/.NET】.NET6中全局异常处理
- consul宕机配置丢失_简单的配置死机
- [Leetcode][第95题][JAVA][不同的二叉搜索树 II][递归]
- python 元类 type_python Class:面向对象高级编程 元类:type
- 小狼程序员:工作遐想
- iOS 应用取消时间栏
- PHP的静态变量和引用函数
- 安装虚拟环境和Flask
- The evolved Transformer,进化的变换器
- 常用的monkey命令
- 数据结构 图-关键路径:AOE网络
- 爬取雪球网的新闻数据
- 计算机缺失程序怎么办,win7电脑缺失dll文件软件打不开怎么办
- linux C/C++服务器后台开发面试题总结
- av发行商_如何向发行商推销游戏
- 论语 --- 学而第一
- 平安又开始大面积裁员了,从外包蔓延到内勤!
- 修复vscode 终端字体间隔过大的问题
热门文章
- linux 计划任务格式,linux crontab 定时任务格式和使用方法2019-01-13
- linux 挂载san存储,新手看招:Linux操作系统下挂载SAN资源
- ora-01113 oracle8i,ora-01113解决办法
- Android入门(13)| Android权限 与 内容提供器
- leetcode771. 宝石与石头
- leetcode71. 简化路径 Unix 风格
- 简单暴力到dp的优化(萌新篇)
- 图的基本概念【数据结构】
- 华三交换机如何进入配置_学校机房项目交换机的如何配置,理解这篇,交换机配置不再难...
- AMD 5XXX 系列显卡的 peak bandwidth计算