/** * lru design * @param operators int整型二维数组 the ops * @param k int整型 the k * @return int整型一维数组 */ function LRU( operators , k ) { // write code here let arr = []; let map = new Map(); for(let i = 0;i<operators.length;i++){ if(operators[i][0] === 1){ //set if(map.has(operators[i][1])){ map.delete(operators[i][1]) map.set(operators[i][1],operators[i][2]) }else{ map.set(operators[i][1],operators[i][2]) if(map.size > k){ const firstKey = map.keys().next().value;//把最不常用的缓存清除 map.delete(firstKey); } } }else{ //get if(map.has(operators[i][1])){ arr.push(map.get(operators[i][1])) let temp = map.get(operators[i][1]) map.delete(operators[i][1]); map.set(operators[i][1], temp) }else{ arr.push(-1) } } } return arr } module.exports = { LRU : LRU };