-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathPowerSet.java
58 lines (52 loc) · 1.25 KB
/
PowerSet.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
48
49
50
51
52
53
54
55
56
57
58
package chapter08RecursionAndDynamicProgramming;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* Problem: Write a method to return all subsets of a set.
*
*/
public class PowerSet {
/**
* Method 1: DFS
*/
public List<List<Integer>> subsets(int[] nums) {
Arrays.sort(nums);
List<List<Integer>> res = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
helper(nums, 0, res, cur);
return res;
}
private void helper(int[] nums, int index, List<List<Integer>> res, List<Integer> cur) {
res.add(new ArrayList<Integer>(cur));
for (int i = index; i < nums.length; i++) {
cur.add(nums[i]);
helper(nums, i + 1, res, cur);
cur.remove(cur.size() - 1);
}
}
/**
* Combinatorics
*/
public List<List<Integer>> subsets2(List<Integer> set) {
List<List<Integer>> res = new ArrayList<>();
int max = 1 << set.size(); // compute 2^N
for (int i = 0; i < max; i++) {
List<Integer> cur = convertIntToSet(set, i);
res.add(cur);
}
return res;
}
private List<Integer> convertIntToSet(List<Integer> set, int x) {
List<Integer> res = new ArrayList<>();
int index = 0;
for (int i = x; i > 0; i >>= 1) {
if ((i & 1) == 1) {
res.add(set.get(index));
}
index++;
}
return res;
}
}