暴力破解
var LRUCache = function(capacity) {
this.length = capacity;
this.stack = [];
this.map = new Map();
};
LRUCache.prototype.get = function(key) {
const map = this.map.get(key);
if(map===undefined){
return -1;
}else{
const idx = this.stack.indexOf(key);
delete this.stack[idx];
this.stack.splice(idx,1);
this.stack.push(key);
return map;
}
};
LRUCache.prototype.put = function(key, value) {
const map = this.map.get(key);
if(map===undefined){
if(this.stack.length===this.length){
const shift = this.stack.shift();
this.map.delete(shift);
}
this.map.set(key,value);
this.stack.push(key);
}else{
this.map.set(key,value);
const idx = this.stack.indexOf(key);
this.stack.splice(idx,1);
this.stack.push(key);
}
};