部分排序
思路:双指针,但是该双指针并不是相互关联的双指针。
/** * @param {number[]} array * @return {number[]} */ var subSort = function(array) { if(!array.length){ return [-1, -1] } let left = -1 let right = -1 let min = Number.MAX_SAFE_INTEGER let max = Number.MIN_SAFE_INTEGER for(let i = 0; i < array.length; i++){ if(array[i] >= max){ max = array[i] }else{ right = i } if(array[array.length - 1 - i] <= min){ min = array[array.length - 1 - i] }else{ left = array.length - 1 - i } } return [left, right] };
珠玑运算
标签:24,guess,slice,++,金典,solution,---,let,array From: https://www.cnblogs.com/dgqp/p/17383142.html思路:简单模拟
/** * @param {string} solution * @param {string} guess * @return {number[]} */ var masterMind = function(solution, guess) { let gusTrue =0 for(let i = 0; i < solution.length; i++){ if(solution[i] === guess[i]){ gusTrue ++ solution = solution.slice(0,i) + solution.slice(i + 1) guess = guess.slice(0,i) + guess.slice(i + 1) i-- } } let gusFalse = 0 for(let i = 0; i < solution.length; i++){ let index = guess.indexOf(solution[i]) if(index > -1){ gusFalse++ guess = guess.slice(0, index) + guess.slice(index + 1) } } return [gusTrue, gusFalse] };