Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path78. Subsets.java
More file actions
Latest commit
executable file
·177 lines (150 loc) · 5.44 KB
/
Copy path78. Subsets.java
File metadata and controls
executable file
·177 lines (150 loc) · 5.44 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
M
tags: Array, Backtracking, DFS, BFS, BitManipulation
time: O(2^n)
space: O(2^n)
给一串uniqueintegers, 找到所有可能的subset. result里面不能有重复.
#### DFS
- dfs的两种路子: 1.pick&&skipdfs, 2.forloopdfs
- 1.pick&&skipdfs: 取或者不取 + backtracking. 当level/index到底,return一个list. Bottom-up, reach底部, 才生产第一个solution.
- 2.forloopdfs: forloop + backtracking. 记得:做subset的时候, 每个dfsrecursivecall是一种独特可能,先加进rst. top-bottom: 有一个solution, 就先加上.
- Time&&space: subsetmeansindependentchoiceofeitherpick&¬pick. Youpickntimes: `O(2^n)`, 3ms
- space: O(2^n) results
#### BitManipulation
- n = nums.length, 那么在每一个index, 都是pick / notpick: 0/1
- 考虑subsetindex0/1的bitmap: range的就是 [0000...00 ~ 2^n-1]
- 每一个bitmap就能展现出一个subset的内容: allthe1representspickedindexes
- 做法:
- 1.找出Range
- 2.遍历每一个bitmapcandidate
- 3.对每一个integer的bitrepresentation遍历, 如果是1, addtolist
- time: O(2^n * 2^n) = O(4^n), still3ms, fast.
#### Iterative, BFS
- BFS, 注意考虑如果让oneleveltogeneratenextlevel
- 1.maintainalistofIndexetostorecandidateindexes.
- 2.每一次打开一层candiates, addthemalltoresult
- 3.并且用每一轮的candidates, populatenextlevel, backintoqueue.
- shouldbesameO(2^n), butactualruntime7ms, slower
- O(n) space
```
/*
Given a set of distinct integers, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
*/
// pick&&skip dfs, backtracking,
// bottom-up: reach leaf to save result
classSolution {
publicList<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = newArrayList<>();
if (nums == null || nums.length == 0) returnresult;
dfs(result, newArrayList<>(), nums, 0); // dfs with depth = 0
returnresult;
}
privatevoiddfs(List<List<Integer>> result, List<Integer> list, int[] nums, intdepth) {
if (depth >= nums.length) { // closure case
result.add(newArrayList<>(list));
return;
}
// pick
list.add(nums[depth]);
dfs(result, list, nums, depth + 1);
// backtracking, and move to the not-pick option
list.remove(list.size() - 1);
dfs(result, list, nums, depth + 1);
}
}
// for loop dfs:
// top-down, add each step as solution, as see fit
classSolution {
publicList<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = newArrayList<>();
if (nums == null || nums.length == 0) returnresult; // edge case
List<Integer> list = newArrayList<>();
result.add(newArrayList<>(list));
// dfs with depth = 0
dfs(result, list, nums, 0);
returnresult;
}
privatevoiddfs(List<List<Integer>> result, List<Integer> list, int[] nums, intdepth) {
for (inti = depth; i < nums.length; i++) {
list.add(nums[i]);
result.add(newArrayList<>(list));
dfs(result, list, nums, i + 1);
list.remove(list.size() - 1);
}
}
}
// Bit manipulation
classSolution {
publicList<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = newArrayList<>();
// edge case
if (nums == null || nums.length == 0) {
returnresult;
}
// set bit range
intn = nums.length;
longmaxRange = (long) Math.pow(2, n) - 1;
// for loop for all integer representation of the bit map
for (inti = 0; i <= maxRange; i++) {
List<Integer> list = newArrayList<>();
intindex = 0;
intnum = i;
// bit & each index to find if that index is picked
while (num != 0) {
if ((num & 1) == 1) {
list.add(nums[index]);
}
num = num >> 1;
index++;
}
result.add(list);
}
returnresult;
}
}
// BFS, Queue
/*
DFS: pick or no pick. track level. when level == nums.length, output.
*/
classSolution {
publicList<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = newArrayList<>();
if (nums == null || nums.length == 0) returnresult;
Queue<List<Integer>> queue = newLinkedList<>();
queue.offer(newArrayList<>());
while (!queue.isEmpty()) {
intsize = queue.size();
while (size-- > 0) {
List<Integer> indexRow = queue.poll();
result.add(buildResult(indexRow, nums)); // record result
// populate queue with index
intendIndex = indexRow.size() == 0 ? 0 : indexRow.get(indexRow.size() - 1) + 1;
for (inti = endIndex; i < nums.length; i++) {
indexRow.add(i);
queue.offer(newArrayList<>(indexRow));
indexRow.remove(indexRow.size() - 1); // backtrack
}
}
}
returnresult;
}
privateList<Integer> buildResult(List<Integer> indexRow, int[] nums) {
List<Integer> list = newArrayList<>();
for (intindex : indexRow) list.add(nums[index]);
returnlist;
}
}
```