https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=13&tqId=11159&rp=1&ru=/ta/ coding-interviews&qru=/ta/coding-interviews/question-ranking Test instructions A non-descending array of two arrays, looking for the smallest number of arrays. Topic: Moving a number of elements at the beginning of an array to the end of the array, which we call rotation. Enter a rotation of an incrementing sorted array, outputting the smallest element of the rotated array. For example, the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the smallest element of the array is 1. analysis Http://blog.csdn.net/jacob_007/article/details /52601847 loop method to write dichotomy 1. The decision condition is start+1 less than end,start=0, end=size-1 2. mid=start+ ((End-start) >>1) 3. If DATA[MID] satisfies the condition to return directly, if the data that satisfies the condition is start=mid on the right side of mid, the data that meets the criteria will be end=mid 4 on the left side of mid. Finally, because left and right are adjacent, it is possible to judge, and the final Judgment also includes two cases of Nums.length ==1 and 2. Code Import Java.util.arraylist;public class Solution { public int Minnumberinrotatearray (int [] nums) { if (nums = = NULL | | nums.length = = 0) Return 0; int left = 0;&NBSP;&NBSP;&NBsp; int right = Nums.length-1; int MID = Left; while (left + 1 < right) { mid = left + (right-left)/2; if (Nums[mid] = = Nums[left] && Nums[mid] = = nums[ Right]) return threeposequal (nums,left,right); if (Nums[left] <= nums[mid]) left = mid; else if (Nums[right] >= nums[mid]) right = mid; } if (Nums[left] < nums[ Right]) return Nums[left]; &nbsP; else return Nums[right]; } public int threeposequal (int[] Nums,int left,int right) { int min = Nums[left] ; for (int i = left + 1, left <= right; left++) { &nbs P; if (Nums[left] < min) min = nums[left]; } return min; }}
Sword refers to offer------array, dichotomy---the smallest number of rotated arrays