-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0081-search-in-rotated-sorted-array-ii.py
29 lines (24 loc) · 1.21 KB
/
0081-search-in-rotated-sorted-array-ii.py
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
class Solution:
def search(self, nums: List[int], target: int) -> bool:
low, high = 0, len(nums) - 1
while low <= high:
mid = (low + high) // 2
if nums[mid] == target:
return True
# If we cannot determine the sorted half due to duplicates
if nums[low] == nums[mid]:
low += 1
continue
# If left half is sorted
if nums[low] <= nums[mid]:
if nums[low] <= target <= nums[mid]:
high = mid - 1
else:
low = mid + 1
# If right half is sorted
else:
if nums[mid] <= target <= nums[high]:
low = mid + 1
else:
high = mid - 1
return False