-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path35.js
More file actions
33 lines (27 loc) · 980 Bytes
/
35.js
File metadata and controls
33 lines (27 loc) · 980 Bytes
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
// 35_Search Insert Position
const searchInsert = function (nums, target) {
if (nums.includes(target)) return nums.indexOf(target);
if (target > nums[nums.length - 1]) return nums.length;
if (target < nums[0]) return 0;
const curRange = [0, nums.length - 1];
// 一定會在某兩個數字中間
while (curRange[1] - curRange[0] > 1) {
const midIndex = Math.floor((curRange[0] + curRange[1]) / 2);
if (target > nums[midIndex]) {
curRange[0] = midIndex;
} else {
curRange[1] = midIndex;
}
}
return curRange[1];
};
// const searchInsert = function (nums, target) {
// if (nums.includes(target)) return nums.indexOf(target);
// if (target > nums[nums.length - 1]) return nums.length;
// if (target < nums[0]) return 0;
// for (let i = 0; i < nums.length; i++) {
// if (target > nums[i] && target < nums[i + 1]) return i + 1;
// }
// };
// console.log(searchInsert([1, 3, 5, 6], 2));
searchInsert([1, 3, 5, 6], 2); // 1