题解 | #旋转数组的最小数字#
旋转数组的最小数字
https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
function minNumberInRotateArray(rotateArray) { // write code here let len = rotateArray.length; if(rotateArray[len - 1] < rotateArray[len - 2]){ return rotateArray[len - 1]; } if(rotateArray[0] < rotateArray[1] && rotateArray[len - 1] >= rotateArray[len - 2] && rotateArray[0] < rotateArray[len - 1]){ return rotateArray[0]; } let l = 0; let r = len - 1; let mid = Math.floor(l + (r - l) / 2);; let aimIndex = -1; while(l <= r){ if(rotateArray[mid] < rotateArray[mid - 1]){ return rotateArray[mid]; }else{ if(mid > 0){ mid--; }else{ mid = len - 2; } } } } module.exports = { minNumberInRotateArray : minNumberInRotateArray };