《剑指offer》学习笔记_面试题11_旋转数组的最小数字

《剑指offer》学习笔记_面试题11_旋转数组的最小数字

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        int size = rotateArray.size();
        int low=0,high = size-1;
        int mid = -1;
        while(low<high-1){//循环的终止条件是low=high-1,即两个下标相邻
            mid = (low+high)/2;
            //如果信息不足,那么使用顺序查找
            if(rotateArray[low]==rotateArray[mid]&&
              rotateArray[mid]==rotateArray[high]){
                return MinInOrder(rotateArray,low,high);
            } 
            if(rotateArray[low]<=rotateArray[mid])
                low = mid;
            else
                high = mid;
        }
        return rotateArray[high];
    }
    //顺序查找最小值
    int MinInOrder(vector<int>rotateArray,int low,int high){
        int res = rotateArray[low];
        for(int i=low+1;i<=high;i++)
            if(rotateArray[i]<res)res = rotateArray[i];
        return res;
    }
};

《剑指offer》学习笔记_面试题11_旋转数组的最小数字