-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path33.py
More file actions
31 lines (31 loc) · 917 Bytes
/
Copy path33.py
File metadata and controls
31 lines (31 loc) · 917 Bytes
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 search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
i, j = 0, len(nums) - 1
if j < 0:
return -1
while i < j:
mid = i + (j - i) // 2
if nums[mid] < nums[i] and nums[mid] < nums[j]:
j = mid
elif nums[mid] > nums[i] and nums[mid] > nums[j]:
i = mid + 1
else:
break
i = j if nums[i] > nums[j] else i
start, end = 0, i - 1
if i == 0 or target < nums[0]:
start, end = i, len(nums) - 1
while start <= end:
mid = start + (end - start) // 2
if nums[mid] < target:
start = mid + 1
elif nums[mid] > target:
end = mid - 1
else:
return mid
return -1