这道题目和上一道题目描述差不多,唯一多了的陷阱是可能会出现重复的数字,因此判断low 和high的值需要谨慎点,题目描述如下:
Suppose a sorted array is rotated at 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.
The array may contain duplicates.
具体的java代码如下:public class Solution {
public int findMin(int[] num) {
int low = 0;
int high = num.length - 1;
while(low < high){
if(num[low] < num[high]){
break;
}
int mid = low + (high - low)/2;
if(num[mid] > num[high]){
low = mid + 1;
}
else{
if(num[mid] == num[high]){
low ++;
high --;
}
else{
high = mid;
}
}
}
return num[low];
}
}