[leetcode][search] Find Minimum in Rotated Sorted Array II

来源:互联网 发布:遗传算法理解 编辑:程序博客网 时间:2024/05/22 09:13

题目:

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

class Solution {public:    int findMin(vector<int>& nums) {        if(nums.empty()) return 0;        return findMinRecersively(nums, 0, nums.size()-1);    }private:    int findMinRecersively(vector<int>& nums, int start, int end){        if(start == end) return nums[start];        if(start + 1 == end) return min(nums[start], nums[end]);        int mid = (start + end) >> 1;        if(nums[mid] > nums[mid+1]) return nums[mid+1];          return min(findMinRecersively(nums, mid+1, end), findMinRecersively(nums, start, mid));    }};

似乎没有影响
0 0
原创粉丝点击