题解 | #旋转数组的最小数字#
旋转数组的最小数字
https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @return int整型 */ int minNumberInRotateArray(int* nums, int numsLen ) { // write code here return findminNumberInRotateArray(nums, 0, numsLen - 1); } // 1 2 3 4 5 // 3 4 5 1 2 // 5 1 2 5 5 5 5 5 5 // 0 1 2 3 4 5 6 7 8 // left = 0, right = 8 // mid = 4 int findminNumberInRotateArray(int* nums, int left, int right) { if(left == right) return nums[left]; int mid = left + (right - left) / 2; if(nums[mid] > nums[right]) { return findminNumberInRotateArray(nums, mid + 1, right); } else if(nums[mid] == nums[right]) { return findminNumberInRotateArray(nums, left, right - 1); // 4 4 4 3 4 // 4 3 4 4 4 } // 5 5 1 2 3 return findminNumberInRotateArray(nums, left, mid); }