-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path34_Search_for_a_Range.py
125 lines (110 loc) · 3.63 KB
/
34_Search_for_a_Range.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Input: nums = [5,7,7,8,8,10], target = 6
Output: [-1,-1]
主要的难点在于复杂度,因为是logn,所以可以用二分法
"""
class Solution(object):
"""
Input: nums = [5,7,7,8,8,10], target = 8
Output: [3,4]
Input: nums = [5,6,6,8,8,10], target = 7
Output: [-1,-1]
"""
#nums = [4,5,6,8,8,10]; target = 7
def recurssive(self,nums,low,mid,high,target):
print('~~~~~~~~IN')
if low == high:
return low
if target < nums[mid]:
print('~~~~1')
high = mid - 1
elif target > nums[mid]:
print('~~~~2')
low = mid + 1
else:
print('~~~~3')
return mid
mid = (low + high)//2
# so = Solution
self.recurssive(nums,low,mid,high,target)
def searchRange(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
"""
先找一个target,然后从target往左右两边再进行二分查找
"""
low ,high = 0,len(nums)
start, end = -1, -1
if not nums or target > nums[high-1] :return [start, end]
print(nums)
while low <= high:#这里要用=号,因为如果只有一个target时,low=high即target
mid = (low + high)//2
if nums[mid] == target:#找到一个target立刻停止
break
elif nums[mid] > target:
high = mid - 1
else:
low = mid + 1
if nums[mid] != target:[start,end]#如果连target都没找到,那元素不存在
#look4the lowbound'往左找下界,同样也是二分查找
ll,lh = 0,mid
while ll <= lh:
nm = (ll + lh)//2
if nums[nm] == target:#找到下界,继续再二分找
lh = nm - 1
start = nm
else:
ll= nm + 1
#look4the upbound 往右找上界
hl,hh = mid ,len(nums)-1
while hl <= hh:
nm = (hl + hh)//2
if nums[nm] == target:#找到后,继续往中点的右边二分查找
hl = nm + 1
end = nm
else:
hh = nm -1
return [start,end]
def searchRange2(self, nums, target):
start, end = -1, -1
low, high = 0,len(nums)-1
if not nums or target > nums[high] :return [start, end]
while low <= high:
mid = (low + high) // 2
print(low,mid,high)
if nums[mid] == target:
high = mid -1
start = mid
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
low, high = 0,len(nums)-1
while low <= high:
mid = (low + high) // 2
print(low,mid,high)
if nums[mid] == target:
low = mid + 1
end = mid
elif nums[mid] < target:
low = mid + 1
else:
high = mid - 1
return [start,end]
so = Solution()
nums = [4,5,6,8,8,8,8,8,8,10,11,12]; target = 8
# nums = [5,7,7,8,10]; target = 9
# nums = [];target = 1
low = 0
high = len(nums)
mid = (low+high)//2
# print(so.recurssive(nums,low,mid,high,target))
print(so.searchRange2(nums,target))