-
Notifications
You must be signed in to change notification settings - Fork 0
/
threeSum.js
94 lines (81 loc) · 2.08 KB
/
threeSum.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
Given an array nums of n integers, are there elements
a, b, c in nums such that a + b + c = 0? Find all unique triplets
in the array which gives the sum of zero.
Notice that the solution set must not contain duplicate triplets.
*/
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function (nums) {
let results = [];
// Sorts array so duplicates are more easily tracked and avoided
nums.sort((a, b) => {
return a - b;
});
// Base cases:
if (nums.length < 3) return [];
if (nums.length === 3) {
if (
nums.reduce((acc, num) => {
return acc + num;
}, 0) === 0
) {
results.push(nums);
}
return results;
}
// Create a for loop with two pointers inside to track values
for (let i = 0; i < nums.length - 2; i++) {
// initialize pointers on array
// [i:0, j:1, 2, 3, 4, k:5]
// [-1, 0, 1, 2, -1, -4]
let j = i + 1;
let k = nums.length - 1;
// if this is not first iteration and i reaches a duplicate, skip while loops,
// and move onto next full iteration to avoid duplicate values
if (i > 0 && nums[i] === nums[i - 1]) continue;
while (j < k) {
let sum = nums[i] + nums[j] + nums[k];
if (sum === 0) {
results.push([nums[i], nums[j], nums[k]]);
while (nums[j] === nums[j + 1]) j++;
while (nums[k] === nums[k - 1]) k--;
j++;
k--;
} else if (sum < 0) {
j++;
} else {
k--;
}
}
}
return results;
};
// Test one:
// Input: nums = [-1,0,1,2,-1,-4]
// Output: [[-1,-1,2],[-1,0,1]]
console.log(threeSum([-1, 0, 1, 2, -1, -4]));
// Test two:
// Input: nums = []
// Output: []
console.log(threeSum([]));
// Test three:
// Input: nums = [0]
// Output: []
console.log(threeSum([0]));
// Test four:
// Input: nums =[-1, 0, 1, 2, -1, -4, 3, 2, 7, -7]
// Output: [
// [ -7, 0, 7 ],
// [ -4, 1, 3 ],
// [ -4, 2, 2 ],
// [ -1, -1, 2 ],
// [ -1, 0, 1 ]
// ]
console.log(threeSum([-1, 0, 1, 2, -1, -4, 3, 2, 7, -7]));
// Test five:
// Input: nums = [0,0,0]
// Output: [[0,0,0]]
console.log(threeSum([0, 0, 0]));