剑指OFFER----面试题11. 旋转数组的最小数字

链接:https://leetcode-cn.com/problems/xuan-zhuan-shu-zu-de-zui-xiao-shu-zi-lcof/submissions/

思路:

  二分

代码:

class Solution {
public:
    int minArray(vector<int>& numbers) {
        int n = numbers.size() - 1;
        if (n < 0)  return -1;
        while (n > 0 && numbers[n] == numbers[0]) n--;
        if (numbers[n] >= numbers[0]) return numbers[0];
        int l = 0, r = n;
        while (l < r) {
            int mid = l + r >> 1;
            if (numbers[mid] < numbers[0]) r = mid;
            else l = mid + 1;
        }
        return numbers[r];
    }
};

 

posted @ 2020-02-18 18:13  景云ⁿ  阅读(118)  评论(0编辑  收藏  举报