描述
假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。
你需要找到其中最小的元素。
数组中可能存在重复的元素。
样例
给出[4,4,5,6,7,0,1,2] 返回 0
public class Solution {
/**
* @param nums: a rotated sorted array
* @return: the minimum number in the array
*/
public int findMin(int[] nums) {
// write your code here
int n=nums.length;
int mid=0;
int start=0,end=n-1;
while(start<end){
mid=(start+end)/2;
if(nums[mid]<nums[end]){
end=mid;
}else if(nums[mid]>nums[end]){
start=mid+1;
}else{
end=end-1;
}
}
return nums[start];
}
}