forked from hongtaocai/code_interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubsets.java
More file actions
executable file
·27 lines (27 loc) · 815 Bytes
/
subsets.java
File metadata and controls
executable file
·27 lines (27 loc) · 815 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
public class Solution {
public ArrayList<ArrayList<Integer>> subsets(int[] S) {
// Start typing your Java solution below
// DO NOT write main() function
Arrays.sort(S);
ArrayList<ArrayList<Integer>> ans = new ArrayList<ArrayList<Integer>>();
if(S.length==0) return ans;
int kk = 0;
kk = 1 << (S.length);
for(int i=0;i<kk;i++){
ArrayList<Integer> tmp = new ArrayList<Integer>();
int digit = 0;
int k = i;
int index = 0;
while(index<S.length){
digit = k & 1;
k >>= 1;
if(digit==0){
tmp.add(S[index]);
}
index++;
}
ans.add(tmp);
}
return ans;
}
}