-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathSubsets.java
More file actions
30 lines (26 loc) · 1.21 KB
/
Subsets.java
File metadata and controls
30 lines (26 loc) · 1.21 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
// Time Complexity : O(2^(n)) where n is the length of nums array.
// Space Complexity : O(n)
// Did this code successfully run on Leetcode : Yes
// Approach : I followed 0/1 recursion with backtracking and while the base condition is met, before storing the result we store a deep
// copy of it so that the rest of recursion happens without any issues. In no choose and choose condition, index needs to be increased as we dont need
// duplicates in subsets. Once we find the result or we reach the end of the recursive function, we remove the last element in the path so that it can continue recursion.
class Solution {
List<List<Integer>> result;
public List<List<Integer>> subsets(int[] nums) {
this.result = new ArrayList<>();
helper(nums, 0, new ArrayList<>());
return result;
}
private void helper(int[] nums, int index, List<Integer> path){
if(index == nums.length){
result.add(new ArrayList<>(path));//add deep copy to result
return;
}
//don't choose
helper(nums, index+1, path);
//choose
path.add(nums[index]);
helper(nums, index+1, path);//recurse
path.remove(path.size()-1); //backtrack
}
}