-
Notifications
You must be signed in to change notification settings - Fork 0
/
81.py
32 lines (29 loc) · 1.01 KB
/
81.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(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
low, high = 0, len(nums) - 1
return self.binary(nums, target, low, high)
def binary(self, nums, target, low, high):
if low > high:
return False
mid = (low + high) / 2
if nums[mid] == target:
return True
if nums[low] == nums[mid]:
return self.binary(nums, target, low + 1, high)
if nums[low] < nums[mid]:
if nums[mid] > target >= nums[low]:
return self.binary(nums, target, low, mid - 1)
else:
return self.binary(nums, target, mid + 1, high)
if nums[low] > nums[mid]:
if nums[mid] < target <= nums[high]:
return self.binary(nums, target, mid + 1, high)
else:
return self.binary(nums, target, low, mid - 1)
solu = Solution()
print solu.search([1, 3, 5], 4)