Skip to content

Instantly share code, notes, and snippets.

@jiachen247
Created April 13, 2022 04:44
Show Gist options
  • Save jiachen247/357e72add6c40f244f58032710d4038e to your computer and use it in GitHub Desktop.
Save jiachen247/357e72add6c40f244f58032710d4038e to your computer and use it in GitHub Desktop.
# https://leetcode.com/problems/search-insert-position/
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
l = 0
r = len(nums) - 1
while l <= r:
mid = (l + r) // 2
if nums[mid] == target:
return mid
elif nums[mid] < target:
l = mid+1
elif nums[mid] > target:
r = mid-1
return l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment