3Sum - aloalgo

3Sum

Medium

You are given an integer array nums.

Your task is to find all unique triplets [nums[i], nums[j], nums[k]] from nums such that their sum is zero.

Note that:

  • The indices i, j, and k must be distinct.
  • The solution set must not contain duplicate triplets.

Return a list of all such unique triplets. The order of triplets in the output does not matter.

Example 1

Input
[-2, -1, 0, 1, 3]
Output
[
  [-1, 0, 1],
  [-2, -1, 3]
]
Explanation:

The triplets are [-1, 0, 1] because -1 + 0 + 1 = 0, and [-2, -1, 3] because -2 + -1 + 3 = 0.

Example 2

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

No triplet sums to zero.

Example 3

Input
[0, 0, 0]
Output
[
  [0, 0, 0]
]
Explanation:

The only triplet is [0, 0, 0] which sums to zero.

Loading...
Input
[-2, -1, 0, 1, 3]
Output
[
  [-1, 0, 1],
  [-2, -1, 3]
]

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!