-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRotatedSortedArraySearch.py
More file actions
34 lines (29 loc) · 1.09 KB
/
Copy pathRotatedSortedArraySearch.py
File metadata and controls
34 lines (29 loc) · 1.09 KB
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
class BS_RotatedArraySolution(object):
def BSearch(self,nums,target,start,end):
mid = (start+end)/2
#print nums[start],nums[mid]
if nums[mid] == target:
return mid
if start<end and nums[start] == target:
return start
if nums[end] == target:
return end
if start > end:
return -1
if target < nums[mid]:
if (nums[start] <= target) or (nums[start] > target and target<=nums[end] and nums[mid]<nums[end]):
return self.BSearch(nums,target,start,mid-1)
#If target > nums[mid], we can either find the target from first half and second half
#Therefore, we need to search from both array
a = self.BSearch(nums,target,start,mid-1)
if a == -1:
return self.BSearch(nums,target,mid+1,end)
return a
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
if nums:
return self.BSearch(nums,target,0,len(nums)-1)