-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay58.js
More file actions
47 lines (35 loc) · 1.42 KB
/
Day58.js
File metadata and controls
47 lines (35 loc) · 1.42 KB
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
//* Write a function threeSum(nums) that takes an array of integers nums and returns all unique triplets [a, b, c] in the array which give the sum of zero.
//* Note: The solution set must not contain duplicate triplets.
// * Example
// *Input: [-1, 0, 1, 2, -1, -4]
//* Output: [[-1, -1, 2], [-1, 0, 1]]
// *Input: [0, 0, 0]
//* Output: [[0, 0, 0]]
function threeSum(nums) {
nums.sort((a, b) => a - b); // Sort the array
const result = [];
for (let i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue; // Skip duplicates
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;
}
// Test cases
console.log(threeSum([-1, 0, 1, 2, -1, -4])); // Output: [[-1, -1, 2], [-1, 0, 1]]
console.log(threeSum([0, 0, 0])); // Output: [[0, 0, 0]]
console.log(threeSum([1, 2, -2, -1])); // Output: []