1 '''
2 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
3 你可以假设数组中无重复元素。
4 示例 1: 输入: [1,3,5,6], 5 输出: 2
5 示例 2: 输入: [1,3,5,6], 2 输出: 1
6 '''
7
8
9 class Solution:
10 def searchInsert(self, nums, target):
11 """
12 :type nums: List[int]
13 :type target: int
14 :rtype: int
15 """
16 if target in nums:
17 return nums.index(target)
18 else:
19 for i in nums:
20 if target < i:
21 return nums.index(i)
22 else:
23 return len(nums)
24
25
26 if __name__ == '__main__':
27 n = [1, 3, 5, 6]
28 v = 7
29 ret = Solution().searchInsert(n, v)
30 print(ret)