-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubset.java
More file actions
33 lines (28 loc) · 935 Bytes
/
Subset.java
File metadata and controls
33 lines (28 loc) · 935 Bytes
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
package Recursion;
import java.util.ArrayList;
import java.util.List;
/*
78. Subsets
*/
public class Subset {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if(nums.length==0) return res;
List<Integer> subset = new ArrayList<>();
subsetCreator(0,nums,subset,res);
return res;
}
public void subsetCreator(int i, int[]nums,List<Integer> curSet, List<List<Integer>> res){
if(i>= nums.length){
// Create a copy of the current set and add it to the final response
res.add(new ArrayList<>(curSet));
return;
}
// Decision to add num at i to current set
curSet.add(nums[i]);
subsetCreator(i+1,nums,curSet,res);
curSet.remove(curSet.size()-1);
// Decision to not add num at i to current set
subsetCreator(i+1,nums,curSet,res);
}
}