I tried solving the 3sum problem and below is my solution. It may not be the most optimized, but when i run it, the answer in the leetcode is wrong. The answer in my IDE for the same code shows the correct answer.
static List<List<Integer>> result = new ArrayList<>();
static boolean dup;
public List<List<Integer>> threeSum(int[] nums) {
if (nums.length < 3) {
return new ArrayList<>();
}
for (int i = 0; i < nums.length - 2; i++) {
for (int j = i + 1; j < nums.length - 1; j++) {
for (int k = j + 1; k < nums.length; k++) {
if ((nums[i] + nums[j] + nums[k]) == 0) {
for (List<Integer> integers : result) {
if (integers.contains(nums[i])
&& integers.contains(nums[j])
&& integers.contains(nums[k])) {
dup = true;
break;
} else {
dup = false;
}
}
if (!dup) {
result.add(Arrays.asList(nums[i], nums[j], nums[k]));
}
}
}
}
}
return result;
}
}```