Designand implement a data structure for Least Recently Used (LRU) cache. It should support the followingoperations: get andput.
get(key) - Get the value (will always be positive) of the key if thekey exists in the cache, otherwise return -1.
put(key, value) -Set or insert the value if the key is not already present. When the cachereached its capacity, it should invalidate the least recently used item beforeinserting a new item.
Followup:
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
这题让你自己编码实现一个Least Recently Used (LRU) cache,要求get和put的计算时间复杂度都是O(1)
通过这一题我学会了哈希表的使用,unordered_map使用的是哈希表存储,它的查询时间是O(1),这一点一定注意,然后还需要注意的是我们需要保存每一个值的使用事件顺序,所以我们还需要一个数组来存储值的使用事件先和顺序,但是假如是线性表的话插入和删除的时间复杂度都是O(n),肯定不合法,所以只有使用链表,使用链表的话查询需要使用的值的时间还是O(n),还是不合法,所以需要在unordered_map里面存储链表的索引,这样通过哈希表里面的值得到链表的位置才能达到O(1)的时间
另外还有一点就是链表里面需要存储value还需要存储key,因为需要通过list的最后一个元素得到map里面的位置,所以需要存储key
这一题主要的知识点是unordered_map是哈希表,查询时间为O(1),还有就是list.splice的使用方法,有三种,然后还有对于表而言,有一个begin(),有一个end(),有一个front(),还需要记住还有一个back(),表示的是最后一个元素!
class LRUCache {
public:
int capacity;
unordered_map<int, list<pair<int,int>>::iterator> cache;
list<pair<int,int>> store;
LRUCache(int capacity) {
this->capacity = capacity;
}
int get(int key) {
unordered_map<int, list<pair<int, int>>::iterator>::iterator it;
list<pair<int, int>> ::iterator tmp;
it = cache.find(key);
if (it == cache.end())
{
return -1;
}
else
{
tmp = (*it).second;
store.splice(store.begin(), store, tmp);
return (*tmp).second;
}
}
void put(int key, int value) {
unordered_map<int, list<pair<int, int>>::iterator>::iterator it,tmp;
it = cache.find(key);
if (it == cache.end())
{
store.push_front(make_pair(key,value));
cache.insert(make_pair(key, store.begin()));
if (store.size() > capacity)
{
tmp = cache.find(store.back().first);
store.erase((*tmp).second);
cache.erase(tmp);
}
}
else
{
(*(*it).second).second = value;
store.splice(store.begin(), store, (*it).second);
}
}
};