搜索插入位置
题目:搜索插入位置
给定一个排序的整数数组nums和一个整数目标值target,请在数组中找到target,并返回其下标。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
请必须使用时间复杂度为O(log n)的算法。
这道题很简单,只需从头遍历找到大于或等于target位置即可
var searchInsert = function(nums, target) {
let i = 0
while(i < nums.length) {
if (target <= nums[i]) {
return i
}
i ++
}
return i
};
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
上次更新: 2025/09/05, 8:09:00