class Solution:
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left = 0
right = len(nums)-1
mid = (left+right)//2
if target<nums[left]:return 0
elif target>nums[right]: return len(nums)
while left<=right:
if target==nums[mid]:return mid
elif target>nums[mid] and target<=nums[mid+1]:return mid+1
elif target<nums[mid]:right=mid
else:left=mid+1
mid = (left+right)//2
Runtime: 36 ms, faster than 98.77% of Python3 online submissions for Search Insert Position.
网友评论