forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubset.java
66 lines (45 loc) · 1.88 KB
/
Subset.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package Algorithms;
import java.util.ArrayList;
import java.util.Arrays;
public class Subset {
public static void main(String args[]){
//int[] a = {1, 2, 3};
int[] a = {0};
//a[0] = 1;
//a[1] = 2;
ArrayList<ArrayList<Integer>> result= Subset.subsets(a);
for(int i = 0; i < result.size(); i ++){
for(int j = 0; j < result.get(i).size(); j ++)
{
System.out.printf("i:%d, j:%d, data: %d", i, j, result.get(i).get(j));
}
}
return;
}
public static ArrayList<ArrayList<Integer>> subsets(int[] S) {
Arrays.sort(S);
return subsets_help(S, S.length);
}
// length: the length of the S array.
private static ArrayList<ArrayList<Integer>> subsets_help(int[] S, int length) {
ArrayList<ArrayList<Integer>> subset_minus1 =
new ArrayList<ArrayList<Integer>>();
if (length == 0){
subset_minus1.add(new ArrayList<Integer>());
return subset_minus1;
}
subset_minus1 = subsets_help(S, length - 1);
System.out.printf("length is :%d subset_minus1.size()%d\n", length, subset_minus1.size());
int max_lastlevel = subset_minus1.size();
for(int i = 0; i < max_lastlevel; i ++){
// copy a set to a new set.
ArrayList<Integer> listTmp = (ArrayList<Integer>)subset_minus1.get(i).clone();
// add the new element
listTmp.add(S[length - 1]);
//System.out.printf("S[length - 1] is :%d\n", S[length - 1]);
subset_minus1.add(listTmp);
//System.out.printf("listTmp is :%s\n", listTmp);
}
return subset_minus1;
}
}