-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay22.java
More file actions
33 lines (26 loc) · 1.01 KB
/
Day22.java
File metadata and controls
33 lines (26 loc) · 1.01 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
import java.util.ArrayList;
import java.util.List;
public class Day22 {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
generateSubsets(nums, 0, new ArrayList<>(), result);
return result;
}
private void generateSubsets(int[] nums, int index, List<Integer> current, List<List<Integer>> result) {
result.add(new ArrayList<>(current));
for (int i = index; i < nums.length; i++) {
current.add(nums[i]);
generateSubsets(nums, i + 1, current, result);
current.remove(current.size() - 1);
}
}
public static void main(String[] args) {
Day22 subsets = new Day22();
int[] nums = {1, 2, 3};
List<List<Integer>> result = subsets.subsets(nums);
System.out.println("Subsets of " + java.util.Arrays.toString(nums) + ":");
for (List<Integer> subset : result) {
System.out.println(subset);
}
}
}