forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path78_subsets.java
39 lines (32 loc) · 1.12 KB
/
78_subsets.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
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
if(nums == null) return res;
res.add(new ArrayList<Integer>());
for(int i = 0; i < nums.length; i++){
int size = res.size();
for(int j = 0; j < size; j++){
List<Integer> copy = new ArrayList<Integer>(res.get(j));
copy.add(nums[i]);
res.add(copy);
}
}
return res;
}
}
public class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> list = new ArrayList<Integer>();
dfs(nums, 0, res, list);
return res;
}
public void dfs(int[] nums, int pos, List<List<Integer>> res, List<Integer> list){
res.add(new ArrayList<Integer>(list));
for(int i = pos; i < nums.length; i++){
list.add(nums[i]);
dfs(nums, i+1, res, list);
list.remove(list.size()-1);
}
}
}