-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsets.java
42 lines (38 loc) · 1006 Bytes
/
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
40
41
42
// Link: https://leetcode.com/problems/subsets/
// Difficulty: Medium
/*
* 0ms, 100%
*
* DFS
* - How many layers the recursion tree has: n (which is the length of nums)
* - How mamy branches each node has: 2
* i.e., we have n positions to try, and in each position, we have two choices: whetehr this number is in
* the subset or not.
*/
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> ans = new ArrayList<>();
dfs(nums, 0, new ArrayList<>(), ans);
return ans;
}
private void dfs(int[] nums, int pos, List<Integer> cur, List<List<Integer>> ans) {
if (pos == nums.length) {
ans.add(new ArrayList<>(cur));
return;
}
// Add the current number into the subset
cur.add(nums[pos]);
dfs(nums, pos + 1, cur, ans);
cur.remove(cur.size() - 1);
// Not add the current number into the subset
dfs(nums, pos + 1, cur, ans);
}
}
/*
* Time complexity: O(2^n)
*
* Space complexity: O(n)
*
* NOTES:
*
*/