35. 搜索插入位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left = 0
right = len(nums)-1
while left<=right:
half = left+(right-left)//2
if nums[half]<target:
left = half+1
if nums[half]>target:
right = half-1
if nums[half]==target:
return half
if target>nums[half]:
return half+1
else:
return half
# for id in range(len(nums)):
# if target - nums[id]<0:
# return id