#include
#include
#include
using namespace std;
class LRUCache
{
private:
int _cap = 0;
list<pair<int, int>> _cache;
unordered_map<int, list<pair<int, int>>::iterator> _map;
public:
int get(int key)
{
if (_map.count(key) > 0)
{
auto temp = *_map[key];
_cache.erase(_map[key]);
_cache.push_front(temp);
_map[key] = _cache.begin();
return temp.second;
}
return -1;
}
void put(int key, int value)
{
if ( _map.count(key) > 0)
{
_cache.erase(_map[key]);
_map.erase(key);
}
else if(_cache.size() == _cap)
{
_cache.pop_back();
_map.erase(key);
}
_cache.push_front(pair<int, int>(key,value));
_map[key] = _cache.begin();
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43