二分查找扩充

题目

  • 旋转数组的最小数字

代码

class Solution {
public:
    /*
    1)考虑1011
    2)mid只与end对比
    */
    int minNumberInRotateArray(vector<int> rotateArray) {
        if(rotateArray.size()==0)
            return 0;
        int start = 0, end = rotateArray.size()-1;
        while(start<end)
        {
            int mid = start + (end-start)/2;
            if(rotateArray[mid]>rotateArray[end])
                start = mid+1;
            else if(rotateArray[mid]==rotateArray[end])
                end = end-1;
            else
                end = mid;
        }
        return rotateArray[start];
    }
};