博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode 146. LRU Cache
阅读量:6654 次
发布时间:2019-06-25

本文共 2211 字,大约阅读时间需要 7 分钟。

Problem:

Design and implement a data structure for . 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 1cache.put(3, 3);    // evicts key 2cache.get(2);       // returns -1 (not found)cache.put(4, 4);    // evicts key 1cache.get(1);       // returns -1 (not found)cache.get(3);       // returns 3cache.get(4);       // returns 4

 

Solution:

  笔试高频题,我在今年笔试中已经两次碰见该题了。这道题是链表和哈希表的结合。对于这道题有几个基础知识必须了解,一个是链表的迭代器失效问题,和vector不同,链表的删除操作只会使迭代器指向的当前节点失效。第二个是第十行的splice函数,它的原型是void splice (iterator position, list& x, iterator i),其作用是仅将链表x中的i指向的一个节点拼接到position的位置。接下来就很简单了,put函数通过哈希表找到key所在链表中的迭代器,将其删除,然后在链表开头添加新的pair,并将哈希表重新赋值。如果超出限制范围,则在链表和哈希表中删除最后一个节点。对于get函数,通过哈希表找到结果,然后通过splice函数将该节点移动到链表开头。

Code:

 

1 class LRUCache { 2 public: 3     LRUCache(int capacity) { 4         limit = capacity; 5     } 6      7     int get(int key) { 8         auto it = um.find(key); 9         if (it == um.end()) return -1;10         l.splice(l.begin(), l, it->second);11         return it->second->second;12     }13     14     void put(int key, int value) {15         auto iter = um.find(key);16         if(iter != um.end()) 17             l.erase(iter->second);18         l.push_front(make_pair(key,value));19         um[key] = l.begin();20         if(um.size() > limit){21             um.erase(l.rbegin()->first);22             l.pop_back();23         }24     }25 private:26     int limit;27     list
> l;28 unordered_map
>::iterator> um;29 };30 31 /**32 * Your LRUCache object will be instantiated and called as such:33 * LRUCache obj = new LRUCache(capacity);34 * int param_1 = obj.get(key);35 * obj.put(key,value);36 */

 

转载于:https://www.cnblogs.com/haoweizh/p/10262557.html

你可能感兴趣的文章
【转载】C++ Interesting卡常数
查看>>
Jlink-10 pin 的定义(stm32使用)官方定义
查看>>
python对象--其它
查看>>
MySQL 核心参数优化
查看>>
Oracle主键及约束
查看>>
新 任 时 间 表 大 臣
查看>>
VMware View 5.0从菜鸟到高手系列 4 -虚拟桌面模板
查看>>
摄影菜鸟使用的相机镜头术语大全分享
查看>>
XenServer部署系列之06——网络配置
查看>>
Python黑科技:50行代码运用Python+OpenCV实现人脸追踪+详细教程+快速入门+图像识...
查看>>
软件测试质量和效率评价之我见
查看>>
kloxo增加了域名,怎么不能访问?如何重启web服务?
查看>>
Nginx调试入门
查看>>
Centos7安装jdk
查看>>
css的再深入8(更新中···)
查看>>
MySQL锁
查看>>
国学题库整理
查看>>
jquery chosen 插件 动态设置+更新选项值
查看>>
求最大值及其下标
查看>>
战力会议1
查看>>