154. Find Minimum in Rotated Sorted Array II

ollow up for "Find Minimum in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?
Suppose an array sorted in ascending order 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


154. Find Minimum in Rotated Sorted Array II_第1张图片

仍然是在旋转有序数组中查找最小值,但是此时元素允许重复。有可能遇到如图所示的情况。

此时无法继续判断,只能退回到线性遍历的方式。

//顺序遍历辅助函数
int help(vector &num,int start,int end){
        int min_v = num[start];
        for(int i=start+1;i<=end;i++){
            min_v = min(min_v,num[i]);
        }
        return min_v;
    }
    
    int findMin(vector &num) {
        int start=0,end=num.size()-1;
        
        while (start=num[start]) {
                start = mid+1;
            } else {
                end = mid;
            }
        }
        
        return num[start];
    }

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