Skip to interview prep
Apexed

36. 3Sum

MediumTwo pointers

Return every unique triplet [a, b, c] from nums with a + b + c = 0.

Sort the numbers inside each triplet ascending, and return the triplets in ascending order. Do not repeat a triplet.

Example 1

Input: nums = [-1, 0, 1, 2, -1, -4]
Output: [[-1, -1, 2], [-1, 0, 1]]

Constraints

  • 3 ≤ nums.length ≤ 3000

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 4 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

3Sum — coding interview practice — Apexed