3Sum (Triplets Summing to Zero)

Given an array of integers, return all unique triplets [a, b, c] such that a + b + c = 0. Elements in a triplet should be in non-descending order, and no duplicate triplets are allowed.

Input Format

nums = array of integers

Output Format

List of triplets (each sorted) that sum to 0

Constraints

  • 3 <= nums.length <= 3000; -10^5 <= nums[i] <= 10^5

Examples

Example 1:

Input:

nums = [-1,0,1,2,-1,-4]

Output:

[[-1,-1,2],[-1,0,1]]

Explanation:

Two triplets sum to zero: [-1, -1, 2] and [-1, 0, 1].

Loading...
3Sum (Triplets Summing to Zero) - Arrays