-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathThreeSumProblemBrute.java
More file actions
32 lines (29 loc) · 1.06 KB
/
ThreeSumProblemBrute.java
File metadata and controls
32 lines (29 loc) · 1.06 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
package swe;
import java.util.*;
public class ThreeSumProblemBrute {
public static List<List<Integer>> threeSum(int[] nums) {
List<List<Integer>> output = new ArrayList<>();
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
for (int k = j + 1; k < nums.length; k++) {
if (nums[i] + nums[j] + nums[k] == 0) {
Set<Integer> set = new TreeSet<>();
set.add(nums[i]);
set.add(nums[j]);
set.add(nums[k]);
List<Integer> list = new ArrayList<>(set);
output.add(list);
}
}
}
}
return output;
}
public static void main(String[] args) {
int[] nums = {-1, 0, 1, 2, -1, -4};
List<List<Integer>> result = threeSum(nums);
for (List<Integer> triplet : result) {
System.out.println(triplet);
}
}
}