3Sum
Medium
Arrays
Two Pointers
Sorting
Given an array `nums`, return all unique triplets [a, b, c] such that a + b + c = 0.
Input: space-separated integers
Output: triplets, one per line, sorted
Constraints
-1000 <= nums[i] <= 1000
3 <= nums.length <= 3000
No duplicate triplets in output.
Examples
Example 1:
Input: -1 0 1 2 -1 -4
Output:
Example 2:
Input: 0 1 1
Output:
Hints
Sort the array first.
For each element, use two pointers on the rest.
Tests:
Runtime:
Memory:
Test
Input:
Expected:
Got:
Click Run Code to test against sample cases, or Submit to test against all cases.
▲ Console