Search insert position in python using binary search
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
res=0
l=0
r=len(nums)
while(l<r):
m=(l+r)//2
if nums[m]==target:
res=m
break
elif target>nums[l]:
l+=1
res=l
else:
r-=1
return res
Comments
Post a Comment
If you have any doubt you ask me in comment section