Skip to main content

Leetcode - 35. Search Insert Position

· One min read

https://leetcode.com/problems/search-insert-position/

Solution 1

/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var searchInsert = function (nums, target) {
if (target < nums[0]) {
return 0;
}

let targetIndex;

for (let i = 0; i < nums.length; i++) {
if (nums[i] >= target) {
targetIndex = i;
break;
} else {
targetIndex = nums.length;
}
}

return targetIndex;
};