# 154. Find Minimum in Rotated Sorted Array II

__

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.

Example 1:

``````Input: [1,3,5]
Output: 1
``````

Example 2:

``````Input: [2,2,2,0,1]
Output: 0
``````

Note:

``````class Solution {
public:
int findMin(vector<int>& nums) {
int left = 0, right = (int)nums.size() - 1;
while (left < right) {
int mid = left + (right - left) / 2;
if (nums[mid] > nums[right]) left = mid + 1;
else if (nums[mid] < nums[right]) right = mid;
else --right;
}
return nums[right];
}
};
``````

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

Github 同步地址：

https://github.com/grandyang/leetcode/issues/154

Find Minimum in Rotated Sorted Array

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/

https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/discuss/48808/My-pretty-simple-code-to-solve-it

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation