-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreeSum.js
More file actions
82 lines (59 loc) · 1.61 KB
/
Copy paththreeSum.js
File metadata and controls
82 lines (59 loc) · 1.61 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Problem solution to 15. 3Sum
//
// https://leetcode.com/problems/3sum/
let threeSumV1 = function (nums) {
nums = nums.sort((a, b) => a - b);
if (nums.length < 3) return [];
let vector = [];
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) break;
if (i > 0 && nums[i] === nums[i - 1]) continue;
let low = i + 1,
high = nums.length - 1;
while (low < high) {
let sum = nums[i] + nums[low] + nums[high];
if (sum > 0) {
high--;
continue;
} else if (sum < 0) {
low++;
continue;
} else {
vector.push([nums[i], nums[low], nums[high]]);
let lastLow = nums[low],
lastHigh = nums[high];
while (low < high && nums[low] == lastLow) {
low++;
}
while (low < high && nums[high] == lastHigh) {
high--;
}
console.log(lastLow, lastHigh, low, high);
}
}
}
console.log(vector);
};
let threeSumV2 = function (nums) {
nums = nums.sort((a, b) => a - b);
if (nums.length < 3) return [];
const res = [];
const hashMap = new Map();
for (const [i, num] of nums.entries()) {
hashMap.set(num, i);
}
for (let i = 0; i < nums.length; i++) {
if (nums[i] > 0) break;
for (let j = i + 1; j < nums.length - 1; j++) {
let required = -1 * (nums[i] + nums[j]);
if (hashMap.has(required) && hashMap.get(required) > j) {
res.push([nums[i], nums[j], required]);
}
j = hashMap.get(nums[j]);
}
i = hashMap.get(nums[i]);
}
return res;
};
let test1 = [-1, 0, 1, 2, -1, -4];
let res = threeSumV1(test1);