-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSolution.js
42 lines (37 loc) · 828 Bytes
/
Solution.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// https://leetcode.com/problems/search-insert-position/submissions/
/**
* @param {number[]} nums
* @param {number} target
* @return {number}
*/
var searchInsert = function (nums, target) {
let i = 0;
for (i = 0; i < nums.length; i++) {
if (nums[i] == target) {
break;
} else {
if (i == 0 && target < nums[i]) {
return i;
}
if (nums[i] <= target && nums[i + 1] >= target) {
i = i + 1
return i;
}
}
}
return i;
};
/*
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
*/
let nums = [1, 3, 5, 6];
let target = 0;
console.log(searchInsert(nums, target));