Created
April 13, 2022 04:44
-
-
Save jiachen247/357e72add6c40f244f58032710d4038e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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