title: Suppose a rotated sorted array whose starting position is unknown (e.g. 0 1 2 4 5 6 7 May become 4 56 7 0 1 2).
You need to find the smallest of these elements.
You can assume that there are no duplicate elements in the array.
idea: first to exclude three extreme cases, empty, only one element, and the entire array is ordered.
When the sequence of the array is randomly rotated, it is divided into two order to be included in the topic of 4567 and 012, to find the middle number and the last element of the array comparison, if greater than the words to indicate the smallest number on the right side of the middle number, if less than the minimum number in the middle number of the left, and then continue to find by Note: The middle number does not appear equal to the last element, because there is no repeating element in the title.
question: I was thinking that the two arrays can be divided into two parts, two ascending array to find the minimum number, and finally proved wrong; Because the thinking is imprisoned in the dichotomy of the premise is a well-ordered array, the result is not thought to use the following method, this is my reference to other people's occasional, I simplified it again.
Public intFindmin (int[] nums) { //Write your code here if(Nums = =NULL|| Nums.length = = 0)//first, three special cases. return0; if(Nums.length = = 1) returnNums[0]; intLow = 0; intHigh = Nums.length-1; intMID =Low ; while(Low <High ) {Mid= (low + high)/2; if(Nums[mid] > Nums[high]) {//if the middle is greater than the last element that indicates the smallest number on the right of the middle numberLow = mid + 1;//the first half of the array is ruled out}Else if(Nums[mid] < Nums[high]) {//If the middle number is less than the last element, indicating the ascending part of the intermediate number, the most novel is on the left of the middle number.High =mid; } } returnNums[low];
}
Novice algorithm Learning path----dichotomy find Minimum in rotated Sorted Array