-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subsets.java
40 lines (31 loc) · 1.17 KB
/
Subsets.java
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
39
40
/*
*Key point: dfs, a basic question of dfs, good example to practice dfs
* eg. S = {1,2,3}
* []
* [1] <=> [1,2] <=> [1,2,3]
* [2] <=> [2,3]
* [3]
*/
public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
if(S == null)
return null;
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
//empty set is the subset of any set
result.add(new ArrayList<Integer>());
//sort array to statisfy the requirement of non-descending order
Arrays.sort(S);
dfsWorker(S, 0, result, new ArrayList<Integer>());
return result;
}
public void dfsWorker(int[] S, int position, ArrayList<ArrayList<Integer>> result, ArrayList<Integer> temp){
for(int i = position;i<S.length;i++){
temp.add(S[i]);
//call dfsWorker() recursively
dfsWorker(S, i+1, result, temp);
result.add((ArrayList<Integer>) temp.clone());
//remove current element from temp before next interation
temp.remove(Integer.valueOf(S[i]));
}
}
}