Skip to content
CodeShare
All problems

3Sum

Find every triplet of numbers that adds up to zero.

MediumArraysTwo pointersSorting

Given an integer array nums, return every unique triplet [a, b, c] of values taken from three different positions such that a + b + c = 0.

The answer must not contain duplicate triplets. You can return the triplets in any order, and the numbers inside each triplet in any order.

Examples

Example 1
Input
nums = [-1, 0, 1, 2, -1, -4]
Output
[[-1, -1, 2], [-1, 0, 1]]
Example 2
Input
nums = [0, 1, 1]
Output
[]
Why
No three numbers add up to 0.
Example 3
Input
nums = [0, 0, 0]
Output
[[0, 0, 0]]

Constraints

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