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]
]
List<List<Integer>> lists = new ArrayList<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 2; i++){
if(i > 0 && nums[i] == nums[i - 1]) {
continue;
}
int sum = - nums[i];
int j = i + 1;
int k = nums.length - 1;
while(j < k) {
if(nums[j] + nums[k] == sum){
List<Integer> ll = new ArrayList<>();
ll.add(nums[i]);
ll.add(nums[j]);
ll.add(nums[k]);
lists.add(ll);
j++;
k--;
while(j < k && nums[j] == nums[j - 1]) j++;
while(j < k && nums[k] == nums[k + 1]) k--;
}
else if (nums[j] + nums[k] > sum) {
k--;
}else{
j++;
}
}
}
return lists;