-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets.java
More file actions
54 lines (50 loc) · 1.42 KB
/
Copy pathSubsets.java
File metadata and controls
54 lines (50 loc) · 1.42 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
public class Subsets
{
public ArrayList<ArrayList<Integer>> subsets(int s[])
{
Arrays.sort(s);
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> path = new ArrayList<Integer>();
subsets(s,path, 0, result);
return result;
}
public void subsets(int s[], ArrayList<Integer> path, int index ,ArrayList<ArrayList<Integer>> result)
{
@SuppressWarnings("unchecked")
ArrayList<Integer> array = (ArrayList<Integer>) path.clone();
result.add(array);
for (int i = index; i < s.length; i++) {
path.add(s[i]);
subsets(s, path, i+1, result);
path.remove(path.size()-1);
}
// if(s.length == index)
// {
// @SuppressWarnings("unchecked")
// ArrayList<Integer> array = (ArrayList<Integer>) path.clone();
// result.add(array);
// return;
// }
// //不选择当前数
// subsets(s, path, index+1, result);
// //选择当前数
// path.add(s[index]);
// subsets(s, path, index+1, result);
// path.remove(path.size()-1);
}
public static void main(String[] args)
{
Subsets set = new Subsets();
int s[] = {4,1,0};
ArrayList<ArrayList<Integer>> result = set.subsets(s);
for (Iterator<ArrayList<Integer>> iterator = result.iterator(); iterator.hasNext();)
{
ArrayList<Integer> arrayList = (ArrayList<Integer>) iterator.next();
System.out.println(arrayList);
}
}
}