Back to problems Solve on LeetCode → See #167 Two Sum II →

3Sum

LeetCode 15 • Medium • Two Pointers

Input: nums = [-2, -1, 0, 1, 2]  →  Output: [[-2,0,2], [-1,0,1]]
Find all unique triplets that sum to zero. Sort first, then fix i and two-pointer on the rest.

TimeO(n²)outer loop + two-pointer pass
SpaceO(1)or O(log n) for sort
i: Target: Triplets: 0
Fixed (i)
Left
Right
Found triplet
i
Left
Right
Result