-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0078. Subsets
More file actions
40 lines (36 loc) · 1.29 KB
/
0078. Subsets
File metadata and controls
40 lines (36 loc) · 1.29 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
class Solution {
// public List<List<Integer>> subsets(int[] nums) {
// List<List<Integer>> res = new ArrayList<>();
// helper(res, new ArrayList<>(), nums, 0);
// return res;
// }
// public void helper(List<List<Integer>> res, List<Integer> curr, int[] nums, int index) {
// if (index == nums.length) {
// res.add(new ArrayList<>(curr));
// return;
// }
// else {
// curr.add(nums[index]);
// helper(res, curr, nums, index + 1);
// curr.remove(curr.size() - 1);
// helper(res, curr, nums, index + 1);
// return;
// }
// }
public void helper(List<List<Integer>> res, List<Integer> curr, int[] nums, int index) {
if (index >= nums.length + 1) {}
else {
res.add(new ArrayList<>(curr));
for (int i = index; i < nums.length; i++) {
curr.add(nums[i]);
helper(res, curr, nums, i + 1);
curr.remove(curr.size() - 1);
}
}
}
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res= new ArrayList<>();
helper(res, new ArrayList<>(), nums, 0);
return res;
}
}