146. LRU Cache
来源:互联网 发布:淘宝网小米官方旗舰店 编辑:程序博客网 时间:2024/05/24 01:50
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.
O(n)超时:
struct cache{ int key; int value; int time; };class LRUCache{public: vector<cache> cac; static long long t; int capacity; LRUCache(int capacity) { this->capacity=capacity; cac.reserve(capacity); } int get(int key) { for (int s=0;s<cac.size();s++) if (cac[s].key==key) { cac[s].time=t++; return cac[s].value; } return -1; } void set(int key, int value) { int flag=0; int min_key=INT_MAX,c=INT_MAX; for (int s=0;s<cac.size();s++) { if (cac[s].key==key) { cac[s].time=t++; cac[s].value=value; flag=1; } if (c>cac[s].time) { min_key=cac[s].key; c=cac[s].time; } } if (cac.size()>=capacity&&!flag) { for (int s=0;s<cac.size();s++) { if (cac[s].key==min_key) { cac.erase(cac.begin()+s); break; } } } if (!flag&&cac.size()<capacity) { cache *pCache=new cache(); pCache->key=key; pCache->value=value; pCache->time=t++; cac.push_back(*pCache); } }};long long LRUCache::t=-INT_MAX;
之后我改善后,选择map(红黑树)和list(双向链表)能达到O(logn),map查找速度快O(logn),list插入删除O(1)级别。
访问过的放到链表头部,就达到了LRU的要求,尾部就是不满足条件的。
class LRUCache{public: list<pair<int,int> > caches; map<int,list<pair<int,int> >::iterator> mapCache; int capacity; LRUCache(int capacity) { this->capacity=capacity; } int get(int key) { int ret=-1; map<int,list<pair<int,int> >::iterator>::iterator it=mapCache.find(key); if (it!=mapCache.end()) { ret=it->second->second; pair<int,int> list=*(it->second); caches.erase(it->second); caches.push_front(list); mapCache[key]=caches.begin(); } return ret; } void set(int key, int value) { map<int,list<pair<int,int> >::iterator>::iterator mIt=mapCache.find(key); if (mIt!=mapCache.end()) { pair<int,int> list=*(mIt->second); list.second=value; caches.erase(mIt->second); caches.push_front(list); mapCache[key]=caches.begin(); } else { pair<int,int> p=make_pair(key,value); if (mapCache.size()==capacity) { mapCache.erase(mapCache.find(caches.back().first)); caches.pop_back(); caches.push_front(p); mapCache[key]=caches.begin(); } else { caches.push_front(p); mapCache[key]=caches.begin(); } } }};
1 0
- LeetCode 146. LRU Cache
- [leetcode] 146.LRU Cache
- 146. LRU Cache
- leetcode 146. LRU Cache
- Leetcode 146. LRU Cache
- Leetcode - 146. LRU Cache
- Leetcode 146. LRU Cache
- [leetcode] 146. LRU Cache
- Leetcode 146. LRU Cache
- leetcode 146. LRU Cache
- LeetCode-146. LRU Cache
- LeetCode 146. LRU Cache
- 146. LRU Cache
- LeetCode 146. LRU Cache
- 146. LRU Cache
- 146. LRU Cache
- 146. LRU Cache
- 146. LRU Cache
- [noip模拟赛]旅行Pod(spfa)
- 图的深度遍历
- Android 之 EditText ImageView ImageButton控件
- 顺序表应用1:多余元素删除之移位算法
- logback.xml常用配置详解<configuration> and <logger>
- 146. LRU Cache
- Spring 事物机制总结
- Redis 管道出现线程穿插
- React Native几个重要的属性
- 数学
- java内存结构划分与java虚拟机调优
- lightoj 1043 - Triangle Partitioning (数学)
- CodeForces 598A Tricky Sum
- 【校内互侧】Sunshine’s city (lct+线段树)