al

Find Minimum in Rotated Sorted Array

如前节所述,对于旋转数组的分析可使用画图的方法,如下图所示,升序数组经旋转后可能为如下两种形式。

Rotated Array

题解:

最小值可能在上图中的两种位置出现,如果仍然使用数组首部元素作为target去比较,则需要考虑图中右侧情况。使用逆向思维分析,如果使用数组尾部元素分析,则无需图中右侧的特殊情况。

Question: (159) Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Find the minimum element.

You may assume no duplicate exists in the array.

Example
Given [4,5,6,7,0,1,2] return 0

C++

class Solution {
public:
    /**
     * @param num: a rotated sorted array
     * @return: the minimum number in the array
     */
    int findMin(vector<int> &num) {
        if (num.empty()) {
            return -1;
        }

        vector<int>::size_type start = 0;
        vector<int>::size_type end = num.size() - 1;
        vector<int>::size_type mid;
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (num[mid] < num[end]) {
                end = mid;
            } else {
                start = mid;
            }
        }

        if (num[start] < num[end]) {
            return num[start];
        } else {
            return num[end];
        }
    }
};

源码分析:

仅需注意使用num[end]作为判断依据即可,由于题中已给无重复数组的条件,故无需处理num[mid] == num[end]特殊条件。

Find Minimum in Rotated Sorted Array II

Question: (160) Find Minimum in Rotated Sorted Array II

题解:

由于此题输入可能有重复元素,因此在num[mid] == num[end]时无法使用二分的方法缩小start或者end的取值范围。此时只能使用递增start/递减end逐步缩小范围。

C++

class Solution {
public:
    /**
     * @param num: a rotated sorted array
     * @return: the minimum number in the array
     */
    int findMin(vector<int> &num) {
        if (num.empty()) {
            return -1;
        }

        vector<int>::size_type start = 0;
        vector<int>::size_type end = num.size() - 1;
        vector<int>::size_type mid;
        while (start + 1 < end) {
            mid = start + (end - start) / 2;
            if (num[mid] > num[end]) {
                start = mid;
            } else if (num[mid] < num[end]) {
                end = mid;
            } else {
                --end;
            }
        }

        if (num[start] < num[end]) {
            return num[start];
        } else {
            return num[end];
        }
    }
};