Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Explanation:
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
The distinct triplets are [-1,0,1] and [-1,-1,2].
Notice that the order of the output and the order of the triplets does not matter.
Example 2:
Input: nums = [0,1,1]
Output: []
Explanation: The only possible triplet does not sum up to 0.
Example 3:
Input: nums = [0,0,0]
Output: [[0,0,0]]
Explanation: The only possible triplet sums up to 0.
Constraints:
3 <= nums.length <= 3000
-105 <= nums[i] <= 105
Solutions
Solution 1: Sort + Two Pointers
Thinking
The first idea is three nested loops plus a set for uniqueness. Correct, but \(O(n^3)\). \(n\le 3000\) will not pass. Hashing two-sum for each \(i\) reaches \(O(n^2)\), yet duplicates and extra space are messy.
The bottleneck is finding two numbers that sum to the opposite of a fixed value, without repeating triplets. Sort first so duplicates sit together and are easy to skip; two-sum on a sorted array is two pointers in \(O(n)\). If \(nums[i]>0\), everything after it is positive, so the sum cannot be \(0\) anymore.
So we sort, enumerate the first number, and squeeze the rest with two pointers.
We notice that the problem does not require us to return the triplet in order, so we might as well sort the array first, which makes it easy to skip duplicate elements.
Next, we enumerate the first element of the triplet \(nums[i]\), where \(0 \leq i \lt n - 2\). For each \(i\), we can find \(j\) and \(k\) satisfying \(nums[i] + nums[j] + nums[k] = 0\) by maintaining two pointers \(j = i + 1\) and \(k = n - 1\). In the enumeration process, we need to skip duplicate elements to avoid duplicate triplets.
The specific judgment logic is as follows:
If \(i \gt 0\) and \(nums[i] = nums[i - 1]\), it means that the element currently enumerated is the same as the previous element, we can skip it directly, because it will not produce new results.
If \(nums[i] \gt 0\), it means that the element currently enumerated is greater than \(0\), so the sum of three numbers must not be equal to \(0\), and the enumeration ends.
Otherwise, we let the left pointer \(j = i + 1\), and the right pointer \(k = n - 1\). When \(j \lt k\), the loop is executed, and the sum of three numbers \(x = nums[i] + nums[j] + nums[k]\) is calculated and compared with \(0\):
If \(x \lt 0\), it means that \(nums[j]\) is too small, we need to move \(j\) to the right.
If \(x \gt 0\), it means that \(nums[k]\) is too large, we need to move \(k\) to the left.
Otherwise, it means that we have found a valid triplet, add it to the answer, move \(j\) to the right, move \(k\) to the left, and skip all duplicate elements to continue looking for the next valid triplet.
After the enumeration is over, we can get the answer to the triplet.
The time complexity is \(O(n^2)\), and the space complexity is \(O(\log n)\). The \(n\) is the length of the array.