-
Notifications
You must be signed in to change notification settings - Fork 0
/
167. 两数之和 II - 输入有序数组-MEDIUM.js
62 lines (59 loc) · 1.44 KB
/
167. 两数之和 II - 输入有序数组-MEDIUM.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// https://leetcode.cn/problems/two-sum-ii-input-array-is-sorted/
/**
* @param {number[]} numbers
* @param {number} target
* @return {number[]}
*/
const paramNumbers = [2, 3, 5, 7];
const paramTarget = 9;
// 哈希表
var twoSum = function (numbers, target) {
const map = new Map();
for (let i = 0; i < numbers.length; i++) {
const result = target - numbers[i];
if (map.has(result)) {
return [map.get(result) + 1, i + 1];
} else {
map.set(numbers[i], i);
}
}
return [-1, -1];
};
console.log(1, twoSum(paramNumbers, paramTarget));
// 二分法
var twoSum = function (numbers, target) {
for (let i = 0; i < numbers.length; i++) {
let l = i + 1;
let r = numbers.length - 1;
while (l <= r) {
const mid = parseInt((r - l) / 2) + l;
const sum = numbers[i] + numbers[mid];
if (sum == target) {
return [i + 1, mid + 1];
} else if (sum > target) {
r = mid - 1;
} else if (sum < target) {
l = mid + 1;
}
}
}
return [-1, -1];
};
console.log(2, twoSum(paramNumbers, paramTarget));
// 双指针
var twoSum = function (numbers, target) {
let l = 0;
let r = numbers.length - 1;
while (l < r) {
const sum = numbers[l] + numbers[r];
if (sum == target) {
return [l + 1, r + 1];
} else if (sum < target) {
l++;
} else if (sum > target) {
r--;
}
}
return [-1, -1];
};
console.log(3, twoSum(paramNumbers, paramTarget));