3Sum
Find every unique triplet that sums to zero by sorting, fixing one anchor and squeezing a pair on its right.
01The problem
Return all unique triplets [a, b, c] from nums with a + b + c = 0, using three different positions. The result must not contain duplicate triplets, even when the input repeats values.
| Input | Output |
|---|---|
[-1, 0, 1, 2, -1, -4] | [[-1, -1, 2], [-1, 0, 1]] |
[0, 0, 0] | [[0, 0, 0]] |
Constraints: 3 to 3000 values, each between −100,000 and 100,000.
Loading the rest of the lesson…