-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy path09_03_2020_power_set.java
56 lines (48 loc) · 1.92 KB
/
09_03_2020_power_set.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
/**
* Prompt: Prompt: Given an integer array nums of unique elements, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
*/
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> powerset = new ArrayList<>();
// Add and empty subset
powerset.add(new ArrayList<Integer>());
for (int num : nums) {
int length = powerset.size();
for (int i = 0; i < length; i++) {
// Copy the current subset
List<Integer> current = new ArrayList<>(powerset.get(i));
// Add current num to the current subset
current.add(num);
// Add new subset to the powerset
powerset.add(current);
}
}
return powerset;
}
}
// Recursive Solution
// class Solution {
// public List<List<Integer>> subsets(int[] nums) {
// List<List<Integer>> subsets = new ArrayList<>();
// generateSubsets(subsets, new ArrayList<Integer>(), nums, 0);
// return subsets;
// }
// public void generateSubsets(List<List<Integer>> result,
// List<Integer> subset, int[] nums, int index) {
// result.add(new ArrayList<>(subset));
// // Then we generate the remaining subsets and increment i for next iteration
// for (int i = index; i < nums.length; i++) {
// subset.add(nums[i]);
// generateSubsets(result, subset, nums, i + 1);
// subset.remove(subset.size() - 1);
// }
// }
// }
/*
[]
Iter val subsize curr subset
1 1 1 [] ->[1] [ [], [1] ]
2 2 2 [2], [1, 2] [[], [1], [2], [1, 2]]
3 3 4 [3], [1, 3], [2, 3] [1, 2, 3];
*/