lc35. 搜索插入位置


class Solution: def searchInsert(self, nums: List[int], target: int) -> int: if len(nums) == 0: return 0 left = 0 right = len(nums) - 1 result = 0 while left <= right: mid = int((left + right) / 2) if nums[mid] > target: if nums[mid - 1] < target: return mid right = mid - 1 elif nums[mid] < target: if mid == len(nums) - 1: # 数组末端边界条件,需要额外处理 return len(nums) if nums[mid + 1] > target: return mid + 1 left = mid + 1 else: result = mid break return result  
posted @ 2022-04-29 17:28  stronger_el  阅读(17)  评论(0)    收藏  举报