LeetCode: LRU Cache
来源:互联网 发布:2017院士知乎 编辑:程序博客网 时间:2024/05/16 05:28
这个最坑是需求不写明。set的key如果存在的话,value要替换,同时把LRU的位置刷新。
class LRUCache{public:typedef list<int>::iterator ListIt; LRUCache(int capacity):_capacity(capacity) { _data.reserve(_capacity+1); } int get(int key) { auto it = _data.find(key); if (it==_data.end()){ return -1; } _lru.erase(it->second.second); _lru.push_front(key); it->second.second = _lru.begin(); return it->second.first; } void set(int key, int value) { auto it = _data.find(key); if(it!=_data.end()){ it->second.first = value; _lru.erase(it->second.second); _lru.push_front(key); it->second.second=_lru.begin(); return; } if(_capacity > 0){ _capacity --; }else{ int tmpKey = *(--_lru.end()); _lru.pop_back(); _data.erase(tmpKey); } _lru.push_front(key); _data.insert(make_pair(key, make_pair(value, _lru.begin()))); } list<int> _lru; int _capacity; unordered_map<int, pair<int, ListIt>> _data;};
0 0
- LeetCode LRU 缓存 LRU Cache
- LRU Cache | leetcode
- Leetcode: LRU Cache
- LeetCode:LRU Cache
- Leetcode: LRU Cache
- [LeetCode] LRU Cache
- leetcode之LRU Cache
- [LeetCode]LRU Cache
- [LeetCode] LRU Cache
- leetcode LRU Cache
- LeetCode题解:LRU Cache
- LeetCode | LRU Cache
- Leetcode: LRU Cache
- [LeetCode] LRU Cache
- LeetCode OJ:LRU Cache
- Leetcode LRU Cache
- LeetCode - LRU Cache
- [Leetcode] LRU Cache (Java)
- 在MyEclipse中配置tomcat
- 集合和反编译间的深入理解和认识
- IT面试题---如何确定一棵二叉树是另一棵二叉树的子树(2)
- H264 TS/ES
- linux操作命令
- LeetCode: LRU Cache
- Girl_iOS100天学iOS的第十天(单例和通知)
- 批处理学习教程
- 程序员牛人跳槽
- 为什么很多程序员都选择跳槽?
- 跳槽
- 什么样的遍历序列组合可以唯一地创建一棵二叉树
- PHPExcel如何读取超大excel文件
- ubuntu下java 命令行引用jar包的方法