Find Minimum in Rotated Sorted Array II

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.

The array may contain duplicates.

二分查找多了一个条件A[L]<=A[R] 一种情况 A[L]=A[M]=A[R]

class Solution {
public:
    int findMin(vector<int> &num) {
        int L,R;
        L=0;
        R=num.size()-1;
        while(L<R && num[L]>=num[R])
        {
            int M=(L+R)/2;
            if(num[M]>num[R])
            {
                L=M+1;
            }
            else if(num[M]<num[L])
            {
                R=M;
            }
            else
            {
                L=L+1;
            }
        }
        return num[L];

    }
    
};

也可以直接遍历


你可能感兴趣的:(Find Minimum in Rotated Sorted Array II)