First, the topic
1, examining
2. Analysis
After an ordered integer array is flipped into a two-segment increment ordinal group, the smallest element is found.
Second, the answer
1, Ideas:
Method One,
The minimum element is obtained by the dichotomy method.
①, Nums[start] < Nums[end], the array is ordered; return Nums[start];
②, Nums[start] < nums[mid], the first half is ordered, then start = mid + 1;
Otherwise the second half is ordered, then end = mid;
③, finally, return to Nums[start];
Public intFindmin (int[] nums) { intLen =nums.length; if(len = = 1) returnNums[0]; intStart = 0, end = len-1; while(Start <end) { //already orderly if(Nums[start] <Nums[end]) { returnNums[start]; }//int mid = (start + end)/2; //May be overflow intMID = start + ((End-start) >> 1); //first half ordered if(Nums[start] <Nums[mid]) Start= Mid + 1; //The first half is not required; ElseEnd=mid; } returnNums[start]); }
Method Two,
Directly using linear method to find;
Public int findmin (int[] nums) { for (int i = 1; i < nums.length; i++) { c9/>if(Nums[i] < nums[i-1]) return nums[i]; } return nums[0]; }
153. Find Minimum in rotated Sorted Array