class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
n=len(nums)
l,r = 0,n-1
while l <= r:
mid=l+(r-l)/2
if nums[mid] < target:
if mid==n-1:
return n
if nums[mid+1]>target:
return mid+1
else:
l=mid+1
elif nums[mid] >target:
if mid==0:
return 0
if nums[mid-1]<target:
return mid
else:
r=mid-1
else:
return mid
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
n=len(nums)
l,r = 0,n-1
while l <= r:
mid=l+(r-l)/2
if nums[mid] < target:
if mid==n-1:
return n
if nums[mid+1]>target:
return mid+1
else:
l=mid+1
elif nums[mid] >target:
if mid==0:
return 0
if nums[mid-1]<target:
return mid
else:
r=mid-1
else:
return mid