# 35. 搜索插入位置

// 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
// 你可以假设数组中无重复元素
var searchInsert = function(nums, target) {
  const length = nums.length;
  for (let i = 0; i < length; i++) {
    if (nums[i] === target || nums[i] > target) return i;
  }

  return length;
};

console.log(searchInsert([1, 3, 5, 6], 5));
console.log(searchInsert([1, 3, 5, 6], 2));
console.log(searchInsert([1, 3, 5, 6], 7));
console.log(searchInsert([1, 3, 5, 6], 0));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Last Updated: 6/27/2023, 7:40:45 PM