-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_78Subset.java
47 lines (35 loc) · 1.07 KB
/
_78Subset.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
43
44
45
46
47
package com.test.leetcode;
import java.util.*;
class _78Subset {
public static void backtrack(List<List<Integer>> res, List<Integer> ans, int start, int[] nums) {
res.add(new LinkedList<>(ans));
for (int i=start; i<nums.length; i++) {
ans.add(nums[i]);
backtrack(res, ans, i+1, nums);
ans.remove(ans.size()-1);
}
}
public static void backtrack_adv(List<List<Integer>> res, List<Integer> ans, int start, int[] nums){
if (start==nums.length) {
res.add(new LinkedList<>(ans));
} else {
ans.add(nums[start]);
backtrack_adv(res, ans, start+1, nums);
ans.remove(ans.size()-1);
backtrack_adv(res, ans, start+1, nums);
}
}
public static List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new LinkedList<>();
List<Integer> ans = new LinkedList<>();
/*Boolean[] f = new Boolean[nums.length];
Arrays.fill(f, false);*/
backtrack_adv(res, ans, 0, nums);
return res;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] nums = {3,4,6,8};
System.out.println(subsets(nums));
}
}