在数组中搜索或插入位置,使用数据结构二分法。
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
start=0
end=len(nums)-1
if not nums:
return 0
if target<nums[0]:
return 0
if target>nums[-1]:
return len(nums)
while(start<=end):
mid=(start+end)//2
if target==nums[mid] or nums[mid-1]<target<nums[mid]:
return mid
break
elif target>nums[mid]:
start=mid+1
elif target<nums[mid]:
end=mid-1