LeetCode.154 Find Minimum in Rotated Sorted Array II

来源:互联网 发布:数据库服务器cpu 要求 编辑:程序博客网 时间:2024/06/03 22:12

154. Find Minimum in Rotated Sorted Array II

Follow 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 duplicates.

int half(vector<int>& nums, int begin, int end){    if(begin == end){        return nums[begin];    }    if(nums[begin] < nums[end]){        return nums[begin];    }    else{        int p = (begin + end) / 2;        if(nums[begin] <= nums[p]){            return half(nums, p + 1, end);        }        else{            return half(nums, begin, p);        }    }}int findMin(vector<int>& nums){int begin = 0, end = nums.size() - 1;while(nums[begin] == nums[end] && begin < end){begin++;}return half(nums, begin, end);}


阅读全文
0 0
原创粉丝点击