3Sum
Given an integer array nums, return all unique triplets [nums[i], nums[j], nums[k]] with distinct indices i, j, k such that nums[i] + nums[j] + nums[k] == 0. The result must not contain duplicate triplets.
Open official problem prompt ↗Enumerate every distinct set of three values in the array that add up to exactly zero, without listing the same set twice.
Think of a sorted bookshelf: you pin one book, then use a left hand and a right hand at the two ends of the remaining shelf, sliding them inward until their prices plus the pinned book balance a budget of zero.
- Input
- nums = [-1, 0, 1, 2, -1, -4]
- Output
- [[-1, -1, 2], [-1, 0, 1]]
- Why
- (-1)+(-1)+2 = 0 and (-1)+0+1 = 0; every other combination fails to sum to zero or is a duplicate.
3 <= nums.length <= 3000-10^5 <= nums[i] <= 10^5