6. The minimum number of rotation of the array
Title Description
The beginning of an array of several elements moved to the end of the array, the array we call rotation.
A non-descending order of the input array of a rotation of the output rotary 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.
A non-descending order of the input array of a rotation of the output rotary 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.
Act One:
Search violence
of java.util.ArrayList Import; public class Solution { public int minNumberInRotateArray ( int [] Array) { // start to finish scanning, a position of a value greater than a value recorded before IF (be array.length <= 0 ) return 0 ; for ( int J = 0 ; J <be array.length - . 1 ; J ++ ) { IF (Array [J + . 1 ] < Array [J]) { return Array [J + . 1 ]; } } return 0; } }
Act II:
Use of deformation dichotomy
Analysis: binary search variants, there is no specific value to compare. Then compared with the intermediate value and low bit to see in increasing or decreasing sequence, narrow operation.
1. in increments: Move low
2. in decreasing: high down (if so high-1
, you may miss a minimum, because looking for is a minimum)
3. The remaining cases: low ++ narrow range
Special case:
. 1 Import of java.util.ArrayList; 2 public class Solution { . 3 public int minNumberInRotateArray ( int [] Array) { . 4 // start to finish scanning, prior to recording a value greater than a value of the position . 5 IF (be array.length <= 0 ) . 6 return 0 ; . 7 // modified binary search . 8 int Low = 0 , = High be array.length - . 1 ; . 9 int MID; 10 the while (Low < High) { . 11 MID = (High - Low) /2 + low; 12 if(array[low] < array[high]) 13 return array[low]; 14 if(array[mid] > array[low]){ 15 low = mid + 1; 16 }else if(array[mid] < array[high]){ 17 high = mid; 18 }else{ 19 low++; 20 } 21 } 22 return array[low]; 23 } 24 }