-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreeSumProblemAnother.java
More file actions
38 lines (32 loc) · 1.1 KB
/
ThreeSumProblemAnother.java
File metadata and controls
38 lines (32 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package swe;
import java.util.*;
public class ThreeSumProblemAnother {
public static List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
Set<List<Integer>> allThreeSum = new HashSet<>();
for (int i = 0; i < nums.length - 2; i++) {
int left = i + 1;
int right = nums.length - 1;
while (left < right) {
int sum = nums[i] + nums[left] + nums[right];
if (sum == 0) {
allThreeSum.add(Arrays.asList(nums[i], nums[left], nums[right]));
left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}
return new ArrayList<>(allThreeSum);
}
public static void main(String[] args) {
int[] nums = {-3, -1, 1, 0, 2, 10, -2, 8};
List<List<Integer>> result = threeSum(nums);
for (List<Integer> triplet : result) {
System.out.println(triplet);
}
}
}