#15

3Sum

Medium Sort + two pointers

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return all unique triplets in the array that add up to zero.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: all unique triplets that sum to 0.

Why this works: sort, fix the first number, and the rest is Two Sum II. Skip repeated values so triplets aren't duplicated.

Example: [-1, 0, 1, 2, -1, -4] sorted → [-4, -1, -1, 0, 1, 2]

fix -1: pointers find (-1, 2) and (0, 1)fix the second -1 → skipped (same value)result: [-1, -1, 2], [-1, 0, 1]

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Using a HashSet of lists to remove duplicates — it works but hides the cleaner skip logic and is slower.