3Sum

Given an integer array nums, return all unique triplets [nums[i], nums[j], nums[k]] such that:

  • i, j, and k are distinct indices.
  • nums[i] + nums[j] + nums[k] == 0.

The solution set must not contain duplicate triplets. You may return the triplets in any order.

Example 1
Inputnums = [-1,0,1,2,-1,-4]
Output[[-1,-1,2],[-1,0,1]]
The triplets [-1, -1, 2] and [-1, 0, 1] are the unique combinations that sum to 0.
Example 2
Inputnums = [0,1,1]
Output[]
No three distinct elements in the array sum to 0.

Constraints

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

Asked at 59 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate