Problem
Given an array nums
of $n$ integers, are there elements $a$, $b$, $c$ in nums
such that $a + b + c = 0$? Find all unique triplets in the array which gives the sum of zero.
Note:
The solution set must not contain duplicate triplets.
Example:
1 |
|
Explanation
-
Sort the array.
-
If the first element is greater than 0 or the last element is less than 0, we know that we can’t form a 0.
-
Loop through the array start from the first element, we fix the first element, then use two pointers technique to find two elements that sum to targetVal-fixVal until break out the left < right condition. While looping, we need to ignore the same fix number and left and right values.
Solution
1 |
|