-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC78-Subsets.java
More file actions
31 lines (24 loc) · 836 Bytes
/
Copy pathLC78-Subsets.java
File metadata and controls
31 lines (24 loc) · 836 Bytes
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
import java.util.*;
/**
* Solution for LeetCode Problem 78
* Time Complexity:
* Space Complexity:
*/
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
// Empty case
if(nums == null && nums.length < 1) {return result;}
backtrack(nums, new ArrayList<>(), result, 0);
return result;
}
private void backtrack(int[] input, List<Integer> currentList, List<List<Integer>> result, int startIndex) {
result.add(new ArrayList<>(currentList));
int size = currentList.size();
for(int i=startIndex; i<input.length; i++) {
currentList.add(input[i]);
backtrack(input, currentList, result, i+1);
currentList.remove(currentList.size() - 1);
}
}
}