forked from algorithm019/algorithm019
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets2.java
More file actions
40 lines (32 loc) · 944 Bytes
/
Subsets2.java
File metadata and controls
40 lines (32 loc) · 944 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
32
33
34
35
36
37
38
39
40
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;
/**
* Description: 子集
* Date: 2020-11-17
* Time: 10:48 PM
*/
public class Subsets2 {
public List<List<Integer>> subsets(int[] nums) {
int len = nums.length;
List<List<Integer>> res = new ArrayList<>();
if (len == 0) {
return res;
}
Deque<Integer> path = new ArrayDeque<>();
dfs(nums,len,0,path, res);
return res;
}
private void dfs(int[] nums, int len,int begin,Deque<Integer> path,List<List<Integer>> res) {
res.add(new ArrayList<>(path));
for (int i = begin; i < len; i++) {
path.addLast(nums[i]);
dfs(nums, len, i + 1, path, res);
path.removeLast();
}
}
public static void main(String[] args) {
System.out.println(new Subsets2().subsets(new int[]{1,2,3}));
}
}