leetcode153. 寻找旋转排序数组中的最小值
使用二分法。中间值如果小于等于最右边的值,去左边部分找;中间值如果大于最右边的值,去右半部分找。
def rotate_array_min(nums):
n = len(nums)
low = 0
high = n - 1
while low < high:
mid = low + (high - low) // 2
if nums[mid] < nums[high]:
high = mid
elif nums[mid] > nums[high]:
low = mid + 1
else:
high -= mid
res = nums[low]
return res
nums = [1, 2, 3, 4, 5, 1, 2, 3]
# nums = [3, 100, 200, 3]
res = rotate_array_min(nums)
print(res) # 1