-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path081. Search in Rotated Sorted Array II.py
41 lines (33 loc) · 1.39 KB
/
081. 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
30
31
32
33
34
35
36
37
38
39
40
41
from bisect import bisect_left
class Solution(object):
def searchHelper(self, nums, target, first, last):
if last-first <= 3:
return any( nums[x]==target for x in range(first, last+1) )
if nums[first] < nums[last]:
pos = bisect_left(nums, target, lo=first, hi=last+1)
return pos != last+1 and nums[pos]==target
if target in ( nums[first], nums[last] ):
return True
mid = (last+first)//2
if nums[mid]==target:
return True
if nums[last] == nums[first]:
return self.searchHelper( nums, target, first+1, mid-1 ) or self.searchHelper( nums, target, mid+1, last-1 )
assert nums[last] < nums[first]
if nums[mid]<=nums[last]:
if nums[mid]<target<nums[last]:
return self.searchHelper(nums, target, mid+1, last-1)
else:
return self.searchHelper(nums, target, first+1, mid-1)
elif nums[mid]>=nums[first]:
if nums[first]<target<nums[mid]:
return self.searchHelper(nums, target, first+1, mid-1)
else:
return self.searchHelper(nums, target, mid+1, last-1)
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
return self.searchHelper( nums, target, first=0, last=len(nums)-1 )