Find Minimum in Rotated Sorted Array

Suppose a sorted array is rotated at some pivot unknown to you beforehand.

(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).

Find the minimum element.

Example

Given[4, 5, 6, 7, 0, 1, 2]return0

Note1

就是在两段上升区间, 找到第一个"x",肯定是在右半边

可以定位最后一个元素:小于等于它时不断更新end为mid,否则一直向右找

也可以不定位,直接使用end作为比较的对象,因为它是确保递增的,这半部分右边比左边大

Code1

Note2

如果有重复:

nums[mid] == nums[end], then we should enclose the end by end--, to throw the boundary away

Code2

Last updated