Follow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

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.

思路:duplicate采取夹逼。其他跟I一样。3 3 1 3 -》 夹逼3 1 3 -> 3 1 -> 1

public class Solution {
    public int findMin(int[] nums) {
    if(nums==null||nums.length==0)return 0;
    int low=0,high=nums.length-1;
    while(low<high&&nums[low]>=nums[high]){
        int mid=low+(high-low)/2;
        if(nums[mid]<nums[high]){
            high=mid;
        }else if(nums[mid]>nums[high]){
            low=mid+1;
        }else{
            if(nums[mid]==nums[low])low++;
            else if(nums[mid]==nums[high])high--;
        }
    }
    return nums[low];
}
}

 

posted on 2016-10-13 09:36  Machelsky  阅读(149)  评论(0编辑  收藏  举报