3Sum

Given a list of integers, return a list containing all unique triplets in the list such that the sum of the triplet is zero. Each triplet must be sorted in ascending order, and the resulting list must be sorted lexicographically.

Parameter

  • nums: a list of integers

Result

  • A list of triplets containing all unique triplets that sum up to zero, sorted.

Examples

Example 1

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

Output: [[-1, -1, 2], [-1, 0, 1]]

Example 2

Input: nums = [1, -1, 2, -2, 3, -3, 4, -4]

Output: [[-4, 1, 3], [-3, -1, 4], [-3, 1, 2], [-2, -1, 3]]

Try it yourself

Solution

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro