3Sum

Two Pointers, problem 5 of 7

3Sum

Medium

LC #15

sortingk-sumduplicates

Not attempted yet

Return all unique triplets [a, b, c] of values from nums, taken from three different positions, such that a + b + c == 0.

No triplet may appear twice (as a multiset of values). You can return the triplets, and the numbers inside each triplet, in any order.

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: []

Example 3

Input: nums = [0, 0, 0]
Output: [[0, 0, 0]]

Constraints

  • 3 ≤ len(nums) ≤ 3000
  • -10⁵ ≤ nums[i] ≤ 10⁵

Python

Loading draft…

Test results

9 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 6 hidden

Run examples, then submit all tests.