-
Notifications
You must be signed in to change notification settings - Fork 0
/
33.py
38 lines (37 loc) · 1.17 KB
/
33.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
33
34
35
36
37
38
class Solution:
def search(self, nums: List[int], target: int) -> int:
def findRotationPoint():
l = 0
r = len(nums)-1
while l < r:
mid = l + (r+1-l)//2
if nums[l+1]<nums[l]:
return l+1
elif nums[mid]>nums[l]:
l = mid
else:
r -= 1
if l >= len(nums)-1:
return -1
else:
return l
def binarySearch(l:int, r:int) -> int:
while l < r:
mid = l+(r-l)//2
if nums[mid] > target:
r = mid
elif nums[mid] < target:
l = mid+1
else:
return mid
if l > r:
return -1
else:
if nums[mid] != target:
return -1
return mid
rotationPoint = findRotationPoint()
if rotationPoint == -1:
return binarySearch(0,len(nums))
else:
return max(binarySearch(0,rotationPoint),binarySearch(rotationPoint,len(nums)))