Skip to content

Commit 0878061

Browse files
Create subsets.java
1 parent 0da2c76 commit 0878061

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

subsets.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public List<List<Integer>> subsets(int[] nums) {
3+
List<List<Integer>> result = new ArrayList<>();
4+
backtrack(nums, 0, new ArrayList<>(), result);
5+
return result;
6+
}
7+
8+
private void backtrack(int[] nums, int start, List<Integer> path, List<List<Integer>> result) {
9+
result.add(new ArrayList<>(path));
10+
for (int i = start; i < nums.length; i++) {
11+
path.add(nums[i]);
12+
backtrack(nums, i + 1, path, result);
13+
path.remove(path.size() - 1);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)