-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy path3sum.js
43 lines (36 loc) · 1.31 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
// threeSum.js
/**
* Time Complexity: O(n^2) — The outer loop runs in O(n), and the inner two-pointer approach runs in O(n).
Space Complexity: O(1) — The output list is the only extra space used (not counting input).
*
*
* Finds all unique triplets in the array that sum up to zero.
* @param {number[]} nums - An array of numbers.
* @returns {number[][]} - A list of unique triplets.
*/
function threeSum(nums) {
const result = [];
nums.sort((a, b) => a - b); // Sort the array
for (let i = 0; i < nums.length - 2; i++) {
// Skip duplicate values
if (i > 0 && nums[i] === nums[i - 1]) continue;
let left = i + 1;
let right = nums.length - 1;
while (left < right) {
const sum = nums[i] + nums[left] + nums[right];
if (sum === 0) {
result.push([nums[i], nums[left], nums[right]]);
while (left < right && nums[left] === nums[left + 1]) left++; // Skip duplicates
while (left < right && nums[right] === nums[right - 1]) right--; // Skip duplicates
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
return result; // Return the list of triplets
}
export { threeSum }