Topic:
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.
Problem Solving Ideas:
First, suppose that an array of sorted without rotated is [all-in-one], assuming that we rotate the array through a pivot, then the result may be: [2,3,1], [3,1,2], can be found: Num[low] always greater than (or equal to) num[ High]. Because you were previously an array of sorted, you looked for a pivot in a sorted array to rotate, so for example, the value behind the pivot is greater than the value before the pivot. So based on this discovery, and the dichotomy method to find out. We can solve the problem according to the following judgment. Num[mid] There are two possibilities, if NUM[MID] > Num[high], proving Num[mid] in the interval after rotated, this interval we have just known is greater than the value before pivot, so the minimum value is within the low=mid+1 that interval. Another possibility, Num[mid] <= Num[high], then we can just see that this possibility illustrates Mid~high and is ordered, so the minimum value is within the High=mid range (mid may be the minimum). The minimum value can be found based on this judgment.
The code is as follows:
1 PublicintFindmin (int[] num) {
2intLow = 0, high = num.length-1;
3 while(Low < High && Num[low] >= Num[high]) {
4intMid = (low + high)/2;
5if(Num[mid] > Num[high])
6Low = mid + 1;
7Else
8High = mid;
9}
TenreturnNum[low];
One}
Find Minimum in rotated Sorted Array leetcode java