Find Minimum in Rotated Sorted Array
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.
(确定一个升序序列经过某个位置翻转后的数组的最小值)
Example:
1. 二分查找
二分查找:
- 在
nums[middle] > nums[right]
,如[3, 4, 5, 1, 2],说明最小值必然在右边,因此left = middle + 1
; - 在
nums[middle] <= nums[right]
,如[5, 1, 2, 3, 4],说明最小的必然在左边,因此right = middle
;
具体实现过程如下:
1 | class Solution: |