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.

You may assume no duplicate exists in the array.

二分的思想

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
int findMin(vector<int>& nums) {
int left = 0, right = nums.size() - 1, mid, ret;
if (left == right)
return nums[0];

while (left < right) {
mid = (left + right) / 2;
if (right - left == 1) {
ret = min(nums[left], nums[right]);
break;
}

if (nums[left] < nums[right]) {
ret = nums[left];
break;
}

if (right - left == 2 && nums[mid] > nums[right]) {
ret = nums[right];
break;
}

if (nums[mid] > nums[right]) {
left = mid + 1;
} else {
right = mid;
}
}

return ret;
}
};