forked from rpj911/LeetCode_algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubSet2.java
42 lines (31 loc) · 1.22 KB
/
SubSet2.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
package Algorithms;
import java.util.ArrayList;
import java.util.Arrays;
public class SubSet2 {
public static void main(String[] args){
int[] a = {1,2,3};
ArrayList<ArrayList<Integer>> test = subsets(a);
}
public static ArrayList<ArrayList<Integer>> subsets(int[] num) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
if(num == null || num.length == 0) {
return result;
}
ArrayList<Integer> list = new ArrayList<Integer>();
Arrays.sort(num);
subsetsHelper(result, list, num, 0);
return result;
}
private static void subsetsHelper(ArrayList<ArrayList<Integer>> result,
ArrayList<Integer> list, int[] num, int pos) {
result.add(new ArrayList<Integer>(list));
System.out.printf("generate: %s pos:%d\n", list.toString(), pos);
for (int i = pos; i < num.length; i++) {
list.add(num[i]);
System.out.printf("ADD:%d\n", num[i]);
subsetsHelper(result, list, num, i + 1);
System.out.printf("REMOVE:%d i=%d\n", list.get(list.size() - 1),i);
list.remove(list.size() - 1);
}
}
}