-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35.py
More file actions
22 lines (20 loc) · 750 Bytes
/
35.py
File metadata and controls
22 lines (20 loc) · 750 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Runtime: 48 ms, faster than 29.06% of Python3 online submissions for Search Insert Position.
# Difficulty: Easy
class Solution:
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
return self.si_helper(nums, target, 0, len(nums))
def si_helper(self, nums, target, start, end):
if start == end:
return start
middle = (start + end) // 2
if nums[middle] == target:
return middle
elif nums[middle] < target:
return self.si_helper(nums, target, start + 1, end)
elif nums[middle] > target:
return self.si_helper(nums, target, start, end - 1)