时间超时,有没有大佬能修改成不超时的(不使用map)
设计LRU缓存结构
http://www.nowcoder.com/questionTerminal/e3769a5f49894d49b871c09cadd13a61
运行超时的js
/**
lru design
@param operators int整型二维数组 the ops
@param k int整型 the k
@return int整型一维数组
/
function LRU( operators , k ) {
// write code here
var stack = [];
var out = [];
var indexs = [];
for(var i= 0; i<operators.length;i++){var forEachIndex = 0; if(operators[i][0]==1){ if(stack.length>=k){ var del=0; for(var j=0;j<stack.length-1;j++){ if(stack[del][2]<stack[j+1][2]){ del=j; }else{ del=j+1; } } stack.splice(del,1); } stack.push([operators[i][1],operators[i][2],i]); indexs.push(operators[i][1]); }else if(operators[i][0]==2){ for(var n = 0;n<stack.length;n++){ if(stack[n][0]==operators[i][1]){ //console.log('存在相等项'+i) var index = indexs.indexOf(operators[i][1]); stack[index][2]=i; out.push(stack[n][1]); forEachIndex = forEachIndex + 1; } } if(!forEachIndex){ out.push(-1); //console.log('不存在相等项'+i) } } //console.log(i+"次"+stack) //console.log(i+"次"+out)
}
return out;
}
module.exports = {
LRU : LRU
};