Title Description:
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.
Code implementation:
Public classSolution {/** @param nums:a rotated sorted array * @return: The minimum number in the array*/ Public intFindmin (int[] nums) { if(nums==NULL|| Nums.length==0){ return-1; } intStart = 0; intEnd = Nums.length-1; inttarget =Nums[start]; while(Start+1 <end) { intMid = (start + end)/2; if(Nums[mid] >=target) {Start=mid; } Else{End=mid; } } if(Nums[end] <=target) { returnNums[end]; }Else{ returnTarget; } }}
Note: Binary lookup, take the first element as target, then binary search, intermediate element and target comparison, the final
1. If the array is in the normal order then nums[end] must be greater than target, then the first element is the minimum value of the target;
2. If the array is not in normal order, then nums[end] must be less than the first number;
3. If the array is not in normal order, then Nums[start] becomes the maximum value of the preceding sequence, nums[end] becomes the minimum value of the subsequent sequence.
lintcode-finding the minimum value in a rotated sorted array Java needs review