Original Problem Statement:
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
One of the approach is using the HashSet, What I have try here:
public List> threeSum(int[] nums) {
Set> set = new HashSet<>();
Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
int j = i + 1;
int k = nums.length - 1;
while (j < k) {
int sum = nums[i] + nums[j] + nums[k];
if (sum == 0) {
set.add(Arrays.asList(nums[i], nums[j++], nums[k--]));
} else if (sum > 0) {
k--;
} else if (sum < 0) {
j++;
}
}
}
return new ArrayList<>(set);
}
}