12 · Two Pointers

#15 3Sum

Mediumsolve on LeetCode ↗

frequently reported atAdobeBloombergWalmartAmazonMicrosoftByteDance / TikTok

the problem

In plain words

Return all unique triplets [a, b, c] from nums with a + b + c = 0. The answer must not contain duplicate triplets.

in nums = [-1,0,1,2,-1,-4]

out [[-1,-1,2],[-1,0,1]]

in nums = [0,1,1]

out []

No three numbers add up to 0.

in nums = [0,0,0]

out [[0,0,0]]

  • 3 ≤ n ≤ 3000
  • -10⁵ ≤ nums[i] ≤ 10⁵

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #167
    2. #16
      3Sum Closest
      Mediumin Two Pointers
    3. #1
      Two Sum
      Easyin Hashing