算法复习-常见算法
2021-06-05 10:37:452021-06-05 18:37:53
LRU
class LRUCache {
constructor(max) {
this.max = max;
this.storage = new Map();
}
get(key) {
if (!this.storage.has(key)) return -1;
const tmp = this.storage.get(key);
this.storage.delete(key);
this.storage.set(key, tmp);
return tmp;
}
put(key, value) {
if (this.storage.has(key)) {
this.storage.delete(key);
} else if (this.storage.size >= this.max) {
this.storage.delete(this.storage.keys().next().value);
}
this.storage.set(key, value);
}
}