Skip to contentKEEP THE CRAFTTHE SANCTUARY

Unique Zero-Sum Triplets

Find every distinct triplet of numbers that adds up to zero.

The problem

Implement Solution.threeSum(nums). Return every distinct triplet [a, b, c] of values from nums with a + b + c == 0, where the three values come from three different positions. Two triplets with the same values count as one, so do not return duplicates. The order of the triplets, and of the values inside each triplet, does not matter. For example, [-1, 0, 1, 2, -1, -4] gives [[-1, -1, 2], [-1, 0, 1]].

Starter code

class Solution:
    def threeSum(self, nums: list[int]) -> list[list[int]]:
        pass

Public examples

  1. nums = [-1, 0, 1, 2, -1, -4]gives[[-1, -1, 2], [-1, 0, 1]]
  2. nums = [-4, -3, -1, 0, 1, 3, 4]gives[[-4, 0, 4], [-4, 1, 3], [-3, -1, 4], [-3, 0, 3], [-1, 0, 1]]
  3. nums = [2, 3, 4]gives[]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.