【hard】146. LRU Cache
其实也米有很难……只是c++11的api这么好用的吗
Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get
and put
.
get(key)
- Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1.
put(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.
Follow up:
Could you do both operations in O(1) time complexity?
Example:
LRUCache cache = new LRUCache( 2 /* capacity */ );cache.put(1, 1); cache.put(2, 2); cache.get(1); // returns 1 cache.put(3, 3); // evicts key 2 cache.get(2); // returns -1 (not found) cache.put(4, 4); // evicts key 1 cache.get(1); // returns -1 (not found) cache.get(3); // returns 3 cache.get(4); // returns 4
class LRUCache { public:LRUCache(int capacity) {size = capacity;}int get(int key) {auto it = hash.find(key);if (it != hash.end()){ // find// it->second移到cache.begin()前面的位置cache.splice(cache.begin(), cache, it->second); return it->second->second; // return value }elsereturn -1;}void put(int key, int value) {auto it = hash.find(key);if (it != hash.end()){ // findit->second->second = value;return cache.splice(cache.begin(), cache, it->second);}cache.insert(cache.begin(), make_pair(key, value));hash[key] = cache.begin();if (cache.size() > size){hash.erase(cache.back().first);cache.pop_back();}}private:// C++ STL中,哈希表对应的容器是 unordered_map(since C++ 11)。根据 C++ 11 标准的推荐,用 unordered_map 代替 hash_map.// STL中,map 对应的数据结构是 红黑树。红黑树是一种近似于平衡的二叉查找树,里面的数据是有序的。在红黑树上做查找操作的时间复杂度为 O(logN)。而 unordered_map 对应 哈希表,哈希表的特点就是查找效率高,时间复杂度为常数级别 O(1), 而额外空间复杂度则要高出许多。所以对于需要高效率查询的情况,使用 unordered_map 容器。而如果对内存大小比较敏感或者数据存储要求有序的话,则可以用 map 容器。// Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢.// hash表:存储key//unordered_map<int, list<pair(int, int)>::iterator> hash;unordered_map<int, list<pair<int, int>>::iterator> hash;// 链表:缓存区list<pair<int, int>> cache;// 缓存区大小int size;};/*** Your LRUCache object will be instantiated and called as such:* LRUCache* obj = new LRUCache(capacity);* int param_1 = obj->get(key);* obj->put(key,value);*/
转载于:https://www.cnblogs.com/sherry-yang/p/10807406.html
【hard】146. LRU Cache相关推荐
- 【LeetCode】146. LRU缓存机制
题目描述 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制. 它应该支持以下操作:获取数据 get 和写入数据 put . 获取数据 get(key) - 如果密钥 (key ...
- 【重点】LeetCode 146. LRU Cache
LeetCode 146. LRU Cache 1.Solution1 本博客转载自:http://www.cnblogs.com/grandyang/p/4587511.html 这道题让我们实现一 ...
- 【zz】 现代CPU Cache结构 和 陈首席对CPU Cache的讲解
http://www.tektalk.org/2011/04/14/%e7%8e%b0%e4%bb%a3-cpu-%e4%b8%ad%e7%9a%84-cache-%e7%bb%93%e6%9e%84 ...
- 【LeetCode笔记】146. LRU缓存机制(Java、双向链表、哈希表)
文章目录 题目描述 思路 & 代码 LinkedHashMap 的写法 题目描述 大名鼎鼎的超高频面试题 太感动,在这道题上花了太多时间了,今天终于补上博客了TvT 思路 & 代码 结 ...
- 146. LRU Cache
Title 运用你所掌握的数据结构,设计和实现一个 LRU (最近最少使用) 缓存机制.它应该支持以下操作: 获取数据 get 和 写入数据 put . 获取数据 get(key) -:如果密钥 (k ...
- 【Leetcode】460. LFU Cache
题目地址: https://leetcode.com/problems/lfu-cache/ 实现一个Least Frequently Used (LFU) cache.它是一个类似HashMap的数 ...
- 【kerberos】kinit: Credential cache directory “/run/user/0/krb5cc“ does not exist while getting
前言 在用SUSE 操作系统安装 CM 大数据平台,在 集群开启 kerberos 操作时报错,报错内容如下: kinit: Credential cache directory "/run ...
- 【操作系统】关于LRU算法,FIFO算法,OPT算法页面调度算法及例子
题目:一进程刚获得三个主存块的使用权,若该进程访问页面的次序是{1,3,2,1,2,1,5,1,2,3},采用LRU算法时,缺页数是______次. LRU算法 简介:算法根据数据的历史访问记录来进行 ...
- 【转载】缓存策略-Cache aside旁路缓存
文章目录 Cache aside Cache aside 踩坑 Read through Write through Write behind Cache asideCache aside 也就是旁路 ...
最新文章
- 聊聊底线 | 坏数据与假数据
- 基于Html5的爱情主题网站–表白神器(第二版)
- 22.6. Query 查询
- tableau必知必会之用蝴蝶图(旋风图)实现数据之间对比
- jquery 加法 乘法运算 精确计算函数
- centos6.4下安装python3.6以及对应的django1.11
- Linux Server 安装 raid 1
- sarscape 将dem文件转化成stl_STL文件,一种前处理网格划分技术??
- 深入TextCNN(一)详述CNN及TextCNN原理
- 在火狐(Firefox)浏览器中安装IE Tab插件
- 面试官:兄弟,说说基本类型和包装类型的区别吧
- YOLOv5 NameError: name ‘SPPF‘ is not defined
- 折弯机使用说明书_折弯机操作图解法-如何使用折弯机
- 软件测试的生命周期测试流程
- OpenShift 4 - 使用 Debezium 捕获变化数据,实现MySQL到PostgreSQL数据库同步(附视频)
- GB28181监控视频统一汇聚平台LiveGBS将海康大华华为宇视等厂家监控设备统一接入后如何生成固定播放链接或者固定的流地址可以直接无插件播放或者拉取
- 数字银行的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
- JavaStudy15(27章-满汉楼)—B站韩顺平
- 虹科案例|nanoGUNE应用Onyx系统实现石墨烯电学性质的无损表征
- 【obs-studio开源项目从入门到放弃】video_thread 视频编码线程理解