Search Insert Position
You are given an array nums of distinct integers sorted in ascending order, and an integer target.
If target is in the array, return its index. If it isn't, return the index where it would be inserted so that the array stays sorted. That index can be nums.length when the target is larger than every element.
Your solution must run in O(log n) time.
Constraints
1 <= nums.length <= 10^4-10^4 <= nums[i], target <= 10^4- The values in
numsare distinct and sorted in ascending order.
Examples
Example 1
Input: nums = [1,4,6,9], target = 6 Output: 2
6 is already in the array, at index 2.
Example 2
Input: nums = [1,4,6,9], target = 5 Output: 2
5 belongs between 4 and 6. Inserting it at index 2 keeps the array sorted (6 and 9 shift right).
Example 3
Input: nums = [1,4,6,9], target = 12 Output: 4
12 is larger than every element, so it goes at the very end: index 4, which is nums.length.
JavaScript you'll use: Numbers, Math and integer tricks Arrays: create, read, update
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
function searchInsert(nums, target) {
}
Ctrl/⌘ + ' runs the examples, Ctrl/⌘ + Enter submits. Use console.log freely: output shows under Console. In the editor, press Esc then Tab to move focus out.