class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
left, right = 0, len(nums) - 1
while left <= right:
mid = left + (right - left) // 2
if nums[mid] == target:
return mid
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return left
bisect 모듈 활용 시 한줄로 작성도 가능하다.
class Solution:
def searchInsert(self, nums: List[int], target: int) -> int:
return bisect_left(nums, target)