/
16_three_sum.js
executable file
·49 lines (39 loc) · 1 KB
/
16_three_sum.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
/*
Given a list of integers, write a function that returns sets of three numbers in the list, a,b,c so that a + b + c == 0
threeSum( [-1, 0, 1, 2, -1, -4] );
[-1, -1, 2]
[-1, 0, 1]
- Weather all the numbers are unique or not?
- Can we have duplicate outputs?
[-4, -1, -1, 0, 1, 2]
^ ^ ^
i start end
*/
function threeSum (arr) {
let result = [];
arr.sort();
for (var i = 0; i < arr.length - 3; i++) {
if (i === 0 || arr[i] > arr[i - 1]) {
let start = i + 1;
let end = arr.length -1;
while (start < end) {
if (arr[i] + arr[start] + arr[end] === 0) {
result.push([arr[i], arr[start], arr[end]])
}
if (arr[i] + arr[start] + arr[end] < 0) {
let currentStart = start;
while (arr[start] === arr[currentStart] && start < end) {
start++;
}
} else {
let currentEnd = end;
while (arr[end] === arr[currentEnd] && start < end) {
end--;
}
}
}
}
}
return result;
}
console.log(threeSum( [-1, 0, 1, 2, -1, -4] ));