Suppose a sorted array is rotated on some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4 5 6 7 0 1 2
).
Find the minimum element.
Assume no duplicate exists in the array.
Analysis:
This problem gives an ordered array to find the smallest element in the array, in fact, to find the first position less than the last element appears,
For example 4 5 6 0 1 2 given in the title,
The last element is 2,
Just find the location where the first element less than 2 appears.
The first element is not used as target, because there is a limit to flipping an array, which is to completely not flip, that is 0 1 4 5 6 7,
In this case, an error occurs if the first element is used as the target.
Find the first element that <= Targrt, and the last element is the target.
Public classSolution {//find the first element that <= Targrt, and the last element is the target. Public intFindmin (int[] nums) { if(Nums.length = = 0){ return0; } intStart = 0, end =nums.length; inttarget = Nums[nums.length-1]; intmid; while(Start + 1 <end) {Mid= start + (End-start)/2; if(Nums[mid] <=target) {End=mid; }Else{Start=mid; } } if(Nums[start] <=target) { returnNums[start]; }Else{ returnNums[end]; } }}
Leetcode 153. Find Minimum in rotated Sorted Array