← 返回 microsoft 的题目列表Binary Search Variation
类型:online_judge
microsoft
Given a sorted array of integers nums and a target value target, write a function to search for the insertion position of target in the array. If target is found, return its index. If not found, return the index where it would be inserted to keep the array sorted.
Example 1:
Input: nums = [1,3,5,6], target = 5
Output: 2
Example 2:
Input: nums = [1,3,5,6], target = 2
Output: 1
Example 3:
Input: nums = [1,3,5,6], target = 7
Output: 4
Example 4:
Input: nums = [1,3,5,6], target = 0
Output: 0
Constraints:
The target value is between -2^31 and 2^31 - 1.
All elements are unique.
The array nums does not have duplicate elements.
Example
Input
nums = [1, 3, 5, 6]
target = 5