-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets78.java
More file actions
36 lines (33 loc) · 1.08 KB
/
Copy pathSubsets78.java
File metadata and controls
36 lines (33 loc) · 1.08 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
import java.util.*;
public class Subsets78 {
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
for(int i = 0; i <= nums.length; i++) {
List<Integer> temp = new ArrayList<>();
getSubset(nums, res, temp, i, 0);
}
return res;
}
public static void getSubset(int[] nums, List<List<Integer>> res, List<Integer> temp, int targetSize, int start){
if(temp.size() == targetSize) {
res.add(new ArrayList<>(temp));
}
else{
for(int i = start; i < nums.length; i++){
temp.add(nums[i]);
getSubset(nums, res, temp, targetSize, i + 1);
temp.remove(temp.size() - 1);
}
}
}
public static void main(String[] args) {
int[] nums = new int[]{1,2,3};
List<List<Integer>> res = subsets(nums);
for(List<Integer> list : res){
for(int n : list){
System.out.print(n);
}
System.out.print("\n");
}
}
}