81. 搜索旋转排序数组 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
class Solution:
def search(self, nums: List[int], target: int) -> bool:
n = len(nums)
def find_start()->int: # 找最小。
i = 0
while i<n-1 and nums[i]<=nums[i+1]:
i += 1
return i+1 if i+1!=n else 0
def search_II(nums,target)-> bool:
left,right = 0,len(nums)-1
while left<=right:
mid = (left+right)>>1
if nums[mid]==target:
return True
elif nums[mid]<target:
left = mid+1
else:
right = mid-1
return False
min_idx = find_start()
print("min is: ",min_idx)
ans = search_II(nums[min_idx:],target)
if ans == True:
return True
ans = search_II(nums[0:min_idx],target)
return ans