-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSubsets.java
30 lines (24 loc) · 929 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
import java.util.ArrayList;
import java.util.List;
public class Subsets {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> returnList = new ArrayList<>();
backtrack(nums, new ArrayList<>(), 0, returnList);
return returnList;
}
private void backtrack(int nums[], List<Integer> selected, int selectedCount, List<List<Integer>> returnList){
if(selectedCount == nums.length){
returnList.add(new ArrayList<>(selected));
return;
}
backtrack(nums, selected, selectedCount+1, returnList);
selected.add(nums[selectedCount]);
backtrack(nums, selected, selectedCount+1, returnList);
selected.remove(selected.size()-1);
}
public static void main(String[] args) {
int []array = {0};
Subsets subsets = new Subsets();
System.out.println(subsets.subsets(array));
}
}