154. 寻找旋转排序数组中的最小值 II

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
class Solution:
def findMin(self, nums: List[int]) -> int:
# 经典题目.(下面这个写复杂了,没必要比较三个点。)
# 这个 二分法不是比较中间位置,而是比较两头和中间三个点。
# def search_II():
# left = 0
# right = len(nums)-1
# while left<right:
# mid = (left+right)>>1
# if nums[left]>nums[right]:
# if nums[mid]<=nums[right]:
# right = mid # if mid!=right else mid-1
# elif nums[mid]>nums[right]:
# left = mid+1 # if mid!=left else mid+1
# elif nums[left]==nums[right]:
# left += 1
# elif nums[left]<nums[right]:
# return nums[left]
# return nums[left]
# return search_II()
left = 0
right = len(nums)-1
while left<right:
mid = (left+right)>>1
if nums[mid]>nums[right]:
left = mid+1
elif nums[mid]<nums[right]:
right = mid
else:
right -= 1
return nums[left]