-
Notifications
You must be signed in to change notification settings - Fork 13
/
Solution078.java
42 lines (34 loc) · 1.08 KB
/
Solution078.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
package algorithm.leetcode;
import java.util.LinkedList;
import java.util.List;
/**
* @author: mayuan
* @desc: 子集
* @date: 2019/02/13
*/
public class Solution078 {
public static void main(String[] args) {
List<List<Integer>> result = new Solution078().subsets(new int[]{1, 2, 3});
result.forEach(item -> {
for (int t : item) {
System.out.print(t);
System.out.print(" ");
}
System.out.println();
});
}
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> answer = new LinkedList<>();
List<Integer> oneAnswer = new LinkedList<>();
dfs(answer, oneAnswer, nums, 0);
return answer;
}
private void dfs(List<List<Integer>> answer, List<Integer> oneAnswer, int[] nums, int start) {
answer.add(new LinkedList<>(oneAnswer));
for (int i = start; i < nums.length; ++i) {
oneAnswer.add(nums[i]);
dfs(answer, oneAnswer, nums, i + 1);
oneAnswer.remove(oneAnswer.size() - 1);
}
}
}