Given an array S of n integers, are there elements a, b, c in S 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.
For example, given array S = [-1, 0, 1, 2, -1, -4], A solution set is: [ [-1, 0, 1], [-1, -1, 2] ]
Solution:
For any number x in the input array, we are actually trying to find two other numbers with the sum of -x.
So this problem becomes for each number, find the two sum of its negative.
We first sort the array and then use two pointers to find the two sum.
Notice that we have to do extra work to deal with duplicate numbers.
The time complexity is O(n2).
Code:
public class Solution { public List<List<Integer>> threeSum(int[] nums) { List<List<Integer>> result = new ArrayList<>(); if (nums == null || nums.length < 3) { return result; } Arrays.sort(nums); for (int i = 0; i < nums.length; i++) { if (i > 0 && nums[i] == nums[i - 1]) { continue; } int target = -nums[i]; twoSum(nums, target, i + 1, result); } return result; } public void twoSum(int[] nums, int target, int i, List<List<Integer>> result) { int left = i; int right = nums.length - 1; while (left < right) { if (nums[left] + nums[right] == target) { List<Integer> list = new ArrayList<>(); list.add(-target); list.add(nums[left]); list.add(nums[right]); result.add(list); left++; right--; while (left < right && nums[left] == nums[left - 1]) { left++; } while (right > left && nums[right] == nums[right + 1]) { right--; } } else if (nums[left] + nums[right] < target) { left++; } else { right--; } } } }