class LRUCache {
public:
LRUCache(int capacity) {
}
int get(int key) {
if(pos.find(key)!=pos.end()){
put(key,pos[key]->second);
return pos[key]->second;
}
return -1;
}
void put(int key, int value) {
if(pos.find(key)!=pos.end(0))
recent.erase(pos[key]);
else if (recent.size()>=capacity){
recent.size(recent.back().first);
recent.pop_back;
}
recent.push_front({key,value});
pos[key]=recent.begin();
}
private:
int capacity;
list<pair<int, int>> recent;
unordered_map<int, list<pair<int, int>>::iterator> pos;
};
leetcode 146 c++
最新推荐文章于 2022-08-14 21:17:38 发布