-
Notifications
You must be signed in to change notification settings - Fork 0
/
#81 search.py
32 lines (28 loc) · 936 Bytes
/
#81 search.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
30
31
32
class Solution:
def search(self, nums: List[int], target: int) -> bool:
n = len(nums)
l, r = 0, n - 1
while l <= r:
while l + 1 < n and nums[l] == nums[l + 1]:
l += 1
while r - 1 >= 0 and nums[r] == nums[r - 1]:
r -= 1
mid = (l + r) // 2
val = nums[mid]
if val == target:
return True
# Left is sorted
elif nums[l] <= val:
if val >= target and nums[l] <= target:
# If target is in left side
r = mid - 1
else:
l = mid + 1
# Right is sorted
else:
if val <= target and nums[r] >= target:
# If target is in right side
l = mid + 1
else:
r = mid - 1
return False