-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_solution.js
48 lines (42 loc) · 993 Bytes
/
my_solution.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
/**
* @param {number[]} nums
* @return {number[][]}
*/
const threeSum = (nums) => {
let result = [];
nums.sort((a, b) => a - b);
console.log(nums)
for (let i = 0; i < nums.length; i++) {
if (i > 0 && nums[i] === nums[i - 1]) {
console.log("-------------------> !!!! same as prev")
console.log(nums[i - 1])
console.log(nums[i])
continue;
}
let l = i + 1, r = nums.length - 1;
while (r > l) {
let n = (nums[i] + nums[l] + nums[r]);
console.log("n")
console.log(n)
if (0 === n) {
console.log(`Pushing ${i}`)
result.push([nums[i], nums[l], nums[r]]);
// r--;
l++;
while (nums[l] == nums[l - 1] && r > l) {
l++;
}
} else if (n > 0) {
r--;
} else { // < 0
l++;
}
}
}
console.log("result")
console.log(result)
return (result);
};
// threeSum([0, 0, 0])
// threeSum([-1, 0, 1, 2, -1, -4])
threeSum([-2, 0, 0, 2, 2])