-
Notifications
You must be signed in to change notification settings - Fork 0
/
3sum.js
71 lines (54 loc) · 1.73 KB
/
3sum.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
63
64
65
66
67
68
69
70
71
// https://leetcode.com/problems/3sum/
const threeSum = nums => {
nums = sort(nums);
const result = [];
for (let index = 0; index < nums.length; index++) {
if (nums[index] === nums[index - 1]) continue;
const pairs = twoSum(nums, 0 - nums[index], index);
pairs.forEach(pair => result.push([...pair, nums[index]]));
}
return result;
}
const sort = nums => {
if (nums.length === 1) return nums;
const mid = Math.floor(nums.length / 2);
const left = sort(nums.slice(0, mid));
const right = sort(nums.slice(mid));
return merge(left, right);
}
const merge = (left, right) => {
const result = [];
let i = j = 0;
while (i < left.length && j < right.length)
left[i] < right[j] ? result.push(left[i++]) : result.push(right[j++]);
if (i !== left.length) result.push(...left.slice(i));
if (j !== right.length) result.push(...right.slice(j));
return result;
}
const twoSum = (nums, target, startIndex) => {
const compliments = new Set();
const uniqueSums = new Set();
const pairs = [];
for (let index = startIndex; index < nums.length; index++) {
const num = nums[index];
if (compliments.has(num) && !uniqueSums.has(num)) {
pairs.push([num, target - num]);
uniqueSums.add(num);
continue;
}
compliments.add(target - num);
}
return pairs;
}
nums = [-1,0,1,2,-1,-4];
expected = [[-1,-1,2],[-1,0,1]]
actual = threeSum(nums);
console.assert(threeSum(nums) === expected, '%o', { nums, expected, actual });
nums = [0,1,1];
expected = []
actual = threeSum(nums);
console.assert(threeSum(nums) === expected, '%o', { nums, expected, actual });
nums = [0,0,0];
expected = [[0,0,0]]
actual = threeSum(nums);
console.assert(threeSum(nums) === expected, '%o', { nums, expected, actual });