forked from yuanx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets.java
More file actions
31 lines (26 loc) · 852 Bytes
/
Subsets.java
File metadata and controls
31 lines (26 loc) · 852 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
public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> alist = new ArrayList<ArrayList<Integer>>();
if(S.length== 0) return alist;
Arrays.sort(S);
for(int i=0; i<=S.length; i++){
ArrayList<Integer> ilist = new ArrayList<Integer>();
getsubsets(S, ilist, alist, i, 0);
}
return alist;
}
public void getsubsets(int[] S, ArrayList<Integer> ilist, ArrayList<ArrayList<Integer>> alist, int len, int level){
if(ilist.size() == len){
ArrayList<Integer> temp = new ArrayList<Integer>(ilist);
alist.add(temp);
return;
}
for(int i=level; i<S.length; i++){
ilist.add(S[i]);
getsubsets(S,ilist,alist,len,i+1);
ilist.remove(ilist.size()-1);
}
}
}