FunctionRacerProblemsRace

Three Sum

MediumTwo pointersPython, JavaScript, TypeScript, Java, C++

Find every group of three numbers taken from three different positions of nums that add up to 0.

Each triple must be written in increasing order, like [-1, 0, 1], and no triple may appear twice in the answer. The triples may come back in any order.

Example 1

nums [-1,0,1,2,-1,-4]
returns [[-1,-1,2],[-1,0,1]]

The triples are [-1, -1, 2] and [-1, 0, 1].

Example 2

nums [1,2,3]
returns []

Nothing adds to 0, so return an empty list.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Two pointers problems