旋转数组的最小数字(二分查找)

旋转数组的最小数字

http://www.nowcoder.com/questionTerminal/9f3231a991af4f55b95579b44b7a01ba

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
       if(rotateArray.size()==0)return 0;
       int low=0,high=rotateArray.size()-1,mid;
       while(low<high){
           if(rotateArray[low]<rotateArray[high])return rotateArray[low];//当要查找的数组这样必然递增,直接返回值
           mid = low+(high-low>>1);
           if(rotateArray[mid] > rotateArray[low])low=mid+1;//前半个部分为递增数组。从后半个部分找low=mid+1
           else if(rotateArray[mid] < rotateArray[high])high=mid;//后半个部分递增,则结果在前半个部分high=mid
           else low++;//相等时,无法确定最小值在那一块,只能一步一步找。
       }
       return rotateArray[low];
    }
};

全部评论

相关推荐

Aurora23:属于挂一半,暂时进池子了,隔一段时间没有其他组捞的话就彻底结束了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务