search-insert-position

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
"""https://leetcode.com/problems/search-insert-position/submissions/"""

class Solution:
def searchInsert(self, nums, target):
if target < nums[0]:
return 0
pos = 0
for num in nums:
if target > num:
pos += 1
if target <= num:
return pos
return len(nums)


if __name__ == '__main__':
s = Solution()
nums, target = [1,3,5,6], 5
assert s.searchInsert(nums, target) == 2
nums, target = [1,3,5,6], 2
assert s.searchInsert(nums, target) == 1
nums, target = [1,3,5,6], 7
assert s.searchInsert(nums, target) == 4
nums, target = [1,3,5,6], 0
assert s.searchInsert(nums, target) == 0