你可以假设数组中无重复元素。
示例 1:
输入: [1,3,5,6], 5
输出: 2
示例 2:
输入: [1,3,5,6], 2
输出: 1
示例 3:
输入: [1,3,5,6], 7
输出: 4
示例 4:
输入: [1,3,5,6], 0
输出: 0
class Solution: def searchInsert(self, nums, target): length = len(nums) if nums[0]>target: return 0 if nums[length-1]<target: return length for i in range(length): if nums[i] == target: return i elif nums[i+1] > target and nums[i] != target: return i+1 else: pass if __name__ == '__main__': sl = Solution() r = sl.searchInsert([1,3,5,6], 0) print(r)