Created
March 6, 2019 16:29
-
-
Save mylons/1d899ac5aa76adc5c7f8b278d66f7206 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
class Solution: | |
def searchInsert(self, nums: List[int], target: int) -> int: | |
if target < nums[0]: return 0 | |
elif target > nums[len(nums) - 1]: return len(nums) | |
def recurse(left, right): | |
if left >= right: | |
if nums[left] < target: return left + 1 | |
elif nums[right] < target: return right + 1 | |
return right | |
mid = math.floor((right + left) / 2) | |
num = nums[mid] | |
if target == num: | |
return mid | |
elif num < target: | |
left = mid + 1 | |
else: | |
right = mid - 1 | |
return recurse(left, right) | |
return recurse(0, len(nums) - 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment