Learn SAS from the best-SAS Base, Advanced, and Predictive Modeling

Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You must write an algorithm with O(log n) runtime complexity.

Solution:

class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
for i in range(len(nums)):
if(nums[i]>=target):
return i
return len(nums)

User Avatar

By RichS

2 thoughts on “Leetcode Question and Solution”

Leave a Reply

Your email address will not be published. Required fields are marked *