-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35_Search_Insert_Position.py
More file actions
67 lines (52 loc) · 1.57 KB
/
35_Search_Insert_Position.py
File metadata and controls
67 lines (52 loc) · 1.57 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
56
57
58
59
60
61
62
63
64
65
66
67
"""
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Input: [1,3,5,6], 5
Output: 2
Input: [1,3,5,6], 7
Output: 4
Input: [1,3,5,6], 0
Output: 0
Input: [1,3,5,6], 7
Output: 4
12346789 5
"""
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
for index in range(len(nums)):
if target <= nums[index]:
return index
return len(nums)
def searchInsert_binary_search(self, nums, target):
low = 0
high = len(nums)
if target > nums[high-1]:return high
while low <= high:
mid = (low + high) // 2
# print('low=',low,',mid=',mid,',high=',high)
if target == nums[mid] :
# print('~~~~1')
return mid
elif target > nums[mid] :
# print('~~~~2')
low = mid + 1
index = mid + 1
flag = 'right'
else:# target < nums[mid]
# print('~~~~3')
high = mid - 1
index = mid - 1
flag = 'left'
# print(low,mid,high,'->',flag)
return low
so = Solution()
nums = [1,3,5,6,7,9]; target = 3
# print(so.searchInsert_binary_search(nums,target))
for i in range(0,11):
rs = so.searchInsert_binary_search(nums,i)
print(i,'=>',rs)