-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
39 lines (33 loc) · 1.01 KB
/
Solution.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
package L78;
import java.util.ArrayList;
import java.util.List;
public class Solution {
static int n = 0;
static List<Integer> chosen = new ArrayList<Integer>();
static List<List<Integer>> ans = new ArrayList<List<Integer>>();
public static List<List<Integer>> subsets(int[] nums) {
n = nums.length;
dfs(0, nums);
return ans;
}
public static void dfs(int cur, int[] nums) {
if (cur == n) {
ans.add(new ArrayList<Integer>(chosen));
return;
}
chosen.add(nums[cur]);
dfs(cur + 1, nums);
chosen.remove(chosen.size() - 1);
dfs(cur + 1, nums);
}
public static void main(String[] args) {
int[] nums = {1, 2, 3};
List<List<Integer>> list = subsets(nums);
for (int i = 0; i < list.size(); i++) {
for (int j = 0; j < list.get(i).size(); j++) {
System.out.print(list.get(i).get(j) + " ");
}
System.out.println();
}
}
}