look for the minimum value in the rotated sorted array II
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.
There may be duplicate elements in the array.
Solving
Violence Direct Linear Lookup
Alternatively, linear finds the number corresponding to the first starting descending position
The dichotomy should be considered
Recursion + two points
Public classSolution {/** * @paramnum:a rotated sorted array *@return: The minimum number in the array*/ Public intFindmin (int[] num) { //Write your code here if(num = =NULL|| Num.length = = 0) return0; if(Num.length = = 1) returnNum[0]; returnFindmin (num,0,num.length-1); } Public intFindmin (int[] num,intLeftintRight ) { if(left = =Right )returnNum[left]; if(right = = left + 1) returnmath.min (Num[left],num[right]); intMid = (left + right)/2; if(Num[right] >Num[left])returnNum[left]; Else if(Num[right] = =num [left])returnFindmin (Num,left + 1, right); Else if(Num[mid] >=Num[left])returnfindmin (num,mid,right); Else returnfindmin (Num,left,mid); }}
Java Code
Two points
Public classSolution {/** * @paramnum:a rotated sorted array *@return: The minimum number in the array*/ Public intFindmin (int[] num) { //Write your code here if(num = =NULL|| Num.length = = 0) return0; if(Num.length = = 1) returnNum[0]; intLow = 0; intHigh = Num.length-1; intMID =Low ; while(Low <High ) {Mid= (low + high)/2; if(Num[mid] >Num[high]) { Low= Mid + 1; }Else if(Num[mid] <Num[high]) { High=mid; }Else{ High--; } } returnNum[low]; } }
Java Code
Lintcode: Finding the minimum value in a rotated sorted array II