题目: https://leetcode.com/problems/search-insert-position/
难度:
Medium
找到第一个比target大的值的index,如果没找到则返回len(nums),但是代码中直接返回i值就行了
class Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
i = 0
while nums[i] < target:
i += 1
if i == len(nums):
return i
return iclass Solution(object):
def searchInsert(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
left, right = 0, len(nums) - 1
while left <= right:
mid = left + ((right - left) >> 2)
if nums[mid] < target:
left = mid + 1
else:
right = mid - 1
return left