-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSearchInRotatedSortedArrayII.py
More file actions
55 lines (48 loc) 路 1.43 KB
/
Copy pathSearchInRotatedSortedArrayII.py
File metadata and controls
55 lines (48 loc) 路 1.43 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Follow up for "Search in Rotated Sorted Array":
# What if duplicates are allowed?
#
# Would this affect the run-time complexity? How and why?
#
# Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
#
# (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
#
# Write nums function to determine if nums given target is in the array.
#
# The array may contain duplicates.
#
# Python, Python3 all accepted.
class SearchInRotatedSortedArrayII:
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
if nums is None:
return False
if len(nums) == 1:
return nums[0] == target
start = 0
end = len(nums) - 1
if end == 0:
return False
while start <= end:
mid = start + (end - start) // 2
if nums[mid] == target:
return True
# right half sorted
if nums[mid] < nums[end]:
if nums[mid] < target <= nums[end]:
start = mid + 1
else:
end = mid - 1
# left half sorted
elif nums[mid] > nums[end]:
if nums[start] <= target < nums[mid]:
end = mid - 1
else:
start = mid + 1
else:
end -= 1
return False