题目需求:1)存入key and value, 可以通过key获得value
可以用hashmap来实现(unordered_map)
2)当capacity不够时,remove掉least recently used
可以用double linked-list来实现(list)
class LRUCache {
public:
LRUCache(int capacity) {
size = capacity;
}
int get(int key) {
auto it = hash.find(key);
if(it==hash.end()) return -1;
cache.splice(cache.begin(), cache, it->second);
return it->second->second;
}
void put(int key, int value) {
auto it = hash.find(key);
if(it!=hash.end()){
it->second->second = value;
return cache.splice(cache.begin(), cache, it->second);
}
cache.insert(cache.begin(),