编程练习(第十五周)

来源:互联网 发布:淘宝hd 微淘 编辑:程序博客网 时间:2024/05/16 03:28

题目来源于:https://leetcode.com

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 helper(vector<int>& nums, int l, int r) {    if (l==r) return nums[l];    int m = (l+r)/2;    if (nums[m]<nums[r]) return helper(nums, l, m);    else if (nums[m]>nums[r]) return helper(nums, m+1, r);    else return min(helper(nums, l, m), helper(nums, m+1, r));}int findMin(vector<int>& nums) {    return helper(nums, 0, nums.size()-1);}

原创粉丝点击