-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_insert.py
More file actions
48 lines (42 loc) · 1.06 KB
/
Copy pathfind_insert.py
File metadata and controls
48 lines (42 loc) · 1.06 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
"""[summary]
Returns:
[type]: [description]
"""
from typing import List
class Solution:
"""[summary]
"""
def search(self, nums: List[int], target: int) -> int:
"""[summary]
Args:
nums (List[int]): [description]
target (int): [description]
Returns:
int: [description]
"""
low, high = 0, len(nums)-1
insert_index = 0
while low <= high:
mid = int((low + high) / 2)
if target < nums[mid]:
high = mid-1
insert_index = mid
elif target > nums[mid]:
low = mid+1
insert_index = low
else:
return mid
return insert_index
s = Solution()
RESULT = s.search([1,3,5,6], 5)
print(RESULT)
RESULT = s.search([1,3,5,6], 2)
print(RESULT)
RESULT = s.search([1,3,5,6], 7)
print(RESULT)
RESULT = s.search([1,3,5,6], 0)
print(RESULT)
RESULT = s.search([1], 0)
print(RESULT)
RESULT = s.search([1,3], 2)
print(RESULT)