|
1 | 1 | class Solution {
|
2 | 2 | public List<List<Integer>> subsets(int[] nums) {
|
3 |
| - List<List<Integer>> list = new ArrayList<>(); |
4 |
| - helper(nums, list, new ArrayList<>(), 0); |
5 |
| - return list; |
| 3 | + List<List<Integer>> result = new ArrayList<>(); |
| 4 | + helper(nums, 0, new ArrayList<>(), result); |
| 5 | + return new ArrayList<>(result); |
6 | 6 | }
|
7 |
| - |
8 |
| - private void helper(int[] nums, List<List<Integer>> list, List<Integer> curr, int idx) { |
9 |
| - list.add(new ArrayList<>(curr)); |
10 |
| - if (idx >= nums.length) { |
| 7 | + |
| 8 | + private void helper(int[] nums, int currIdx, List<Integer> currSubset, |
| 9 | + List<List<Integer>> result) { |
| 10 | + result.add(new ArrayList<>(currSubset)); |
| 11 | + if (currIdx >= nums.length) { |
11 | 12 | return;
|
12 | 13 | }
|
13 |
| - for (int i = idx; i < nums.length; i++) { |
14 |
| - curr.add(nums[i]); |
15 |
| - helper(nums, list, curr, i + 1); |
16 |
| - curr.remove(curr.size() - 1); |
| 14 | + for (int i = currIdx; i < nums.length; i++) { |
| 15 | + currSubset.add(nums[i]); |
| 16 | + helper(nums, i + 1, currSubset, result); |
| 17 | + currSubset.remove(currSubset.size() - 1); |
17 | 18 | }
|
18 | 19 | }
|
19 | 20 | }
|
0 commit comments