-
Notifications
You must be signed in to change notification settings - Fork 29
/
Solution78.java
37 lines (28 loc) · 954 Bytes
/
Solution78.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
package backstracking_problem;
import java.util.ArrayList;
import java.util.List;
/**
* 子集
*/
public class Solution78 {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> subsets = new ArrayList<>();
List<Integer> tempSubset = new ArrayList<>();
// 统计不同子集的大小
for (int i = 0; i <= nums.length; i++) {
backTracing(0, i, tempSubset, subsets, nums);
}
return subsets;
}
private void backTracing(int start, int size, List<Integer> tempSubset, List<List<Integer>> subsets, int[] nums) {
if (tempSubset.size() == size) {
subsets.add(new ArrayList<>(tempSubset));
return;
}
for (int i = start; i < nums.length; i++) {
tempSubset.add(nums[i]);
backTracing(i + 1, size, tempSubset, subsets, nums);
tempSubset.remove(tempSubset.size() - 1);
}
}
}