免费的可乐啊# PennySaver - 省钱一族
k*e
1 楼
class LRUCache{
public:
int size;
unordered_map M;
list L;
LRUCache(int capacity) {
size = capacity;
}
int get(int key) {
if (M.find(key)==M.end()) return -1;
L.remove(key);
L.push_back(key);
return M[key];
}
void set(int key, int value) {
if (M.find(key)==M.end()) {
if (M.size() M[key]=value;
L.push_back(key);
}
else {
M.erase(*L.begin());
M[key] = value;
L.pop_front();
L.push_back(key);
}
}
else {
M[key]=value;
L.remove(key);
L.push_back(key);
}
}
};
public:
int size;
unordered_map
list
LRUCache(int capacity) {
size = capacity;
}
int get(int key) {
if (M.find(key)==M.end()) return -1;
L.remove(key);
L.push_back(key);
return M[key];
}
void set(int key, int value) {
if (M.find(key)==M.end()) {
if (M.size()
L.push_back(key);
}
else {
M.erase(*L.begin());
M[key] = value;
L.pop_front();
L.push_back(key);
}
}
else {
M[key]=value;
L.remove(key);
L.push_back(key);
}
}
};