-
Notifications
You must be signed in to change notification settings - Fork 0
/
search_insert.py
30 lines (25 loc) · 1.03 KB
/
search_insert.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
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
def _search_insert(start, stop):
_nums = nums[start:stop]
if not len(_nums):
return start
if len(_nums) == 1:
if _nums[0] == target:
return start
elif _nums[0] < target:
return start + 1
elif start == 0:
return 0
else:
return start - 1
else:
middle_index = start + ((stop - start) // 2)
middle_value = nums[middle_index]
if middle_value == target:
return middle_index
if middle_value < target:
return _search_insert(middle_index, stop)
else:
return _search_insert(start, middle_index)
return _search_insert(0, len(nums))