Subject description:
The beginning of an array of several elements moved to the end of the array, the array we call rotation.
A non-decreasing input array sort of a rotation, output rotation smallest element array.
For example, an array {3,4,5,1,2} {1,2,3,4,5} is a rotation of the array to a minimum.
NOTE: All the elements are given in greater than 0, if the array size is 0, return 0.
Problem-solving ideas:
Dichotomy answer this question,
mid = low + (high - low)/2
We need to consider three cases:
(1)array[mid] > array[high]:
This situation is similar to array [3,4,5,6,0,1,2], in this case a certain minimum number of mid right.
low = mid + 1
(2)array[mid] == array[high]:
This situation is similar to array [1,0,1,1,1] or [1,1,1,0,1], then the minimum number of bad judgment on the left mid
Or right, then I had a a try,
high = high - 1
(3)array[mid] < array[high]:
This situation is similar to array [2,2,3,4,5,6,6], then the minimum number of array necessarily [mid] or left mid
side. Because the right is inevitably increasing.
high = mid
Note that there is a hole: if only the last two numbers range to be queried, then the mid
numerals will be directed at the front
For example array = [4,6]
array[low] = 4 ;array[mid] = 4 ; array[high] = 6 ;
If high = mid - 1, will generate an error, and therefore high = mid
However, the case (1), low = mid + 1 error will not
public int minNumberInRotateArray(int [] array) { int low = 0 ; int high = array.length - 1; while(low < high){ int mid = low + (high - low) / 2; if(array[mid] > array[high]){ low = mid + 1; }else if(array[mid] == array[high]){ high = high - 1; }else{ high = mid; } } return array[low]; }
Problem-solving ideas II:
1, a first non-descending order array element to a minimum value, after the rotation, when the latter element is greater than the previous element, the element described later is minimum
int minNumberInRotateArray public (int [] Array) { IF (Array == null || be array.length == 0) return 0; for (int I = 0; I <-be array.length. 1; I ++) { IF (Array [ I]> array [I +. 1]) { return array [I +. 1]; } } return array [0]; // If not, rotation of the array does not occur, the minimum value of the first element }