Return all unique triplets [a, b, c] from nums with a + b + c = 0. Each triplet sorted ascending; the list of triplets in any order.
Signature
threeSum(nums: int[]) → int[][]Example 1
nums = [-1,0,1,2,-1,-4]
output = [[-1,-1,2],[-1,0,1]]
[-1,-1,2] and [-1,0,1]
Example 2
nums = [0,0,0,0]
output = [[0,0,0]]
only one unique triplet
Constraints
3 ≤ n ≤ 3000-10^5 ≤ nums[i] ≤ 10^5
Answer checking: any order is accepted.
⌘/Ctrl + Enter runs examples
Run checks the examples. Submit runs every hidden test (17, including large inputs that catch slow solutions).