MENU

剑指 Offer 11. 旋转数组的最小数字

March 9, 2022 • 题解,offer

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

class Solution {
public:
    int minArray(vector<int>& numbers) {
        int l = 0, r = numbers.size()-1, mid, minn = 0x3f3f3f3f;

        if (numbers.size() == 0) {
            return 0;
        }

        while (l<=r) {
            mid = (l + r) / 2;
            if (numbers[l] < numbers[mid]) {
                // mid = l;
                l = mid;
            } else if (numbers[r] > numbers[mid]) {
                // mid = r;
                r = mid;
            } else {
                for (int i = 0; i < numbers.size(); i++) {
                    minn = min(minn, numbers[i]);
                }
                return minn;
            }
        }
        // cout << numbers[r] << endl;
        return numbers[l];
    }
};
作者:NorthCity1984
出处:https://grimoire.cn/acm/offer-11.html
版权:本文《剑指 Offer 11. 旋转数组的最小数字》版权归作者所有
转载:欢迎转载,但未经作者同意,必须保留此段声明;必须在文章中给出原文连接;否则必究法律责任

Last Modified: May 2, 2022