forked from USPCodeLabSanca/dev.hire-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78.cpp
More file actions
42 lines (33 loc) · 1.28 KB
/
78.cpp
File metadata and controls
42 lines (33 loc) · 1.28 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
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> powerSet;
vector<vector<int>> bitStrings;
// Generate bit strings for powerset algoirithm
vector<int> baseBitString(nums.size(), 0);
generateBitString(0, nums.size(), baseBitString, bitStrings);
// Generate power set based on bit strings
for (int i = 0; i < bitStrings.size(); i++) {
vector<int> subset;
for (int j = 0; j < bitStrings[i].size(); j++) {
if (bitStrings[i][j] == 1)
subset.push_back(nums[j]);
}
powerSet.push_back(subset);
}
return powerSet;
}
void generateBitString(int i, int n, vector<int>& currentBitString, vector<vector<int>>& allBitStrings) {
if (i == n) {
allBitStrings.push_back(currentBitString);
return;
}
vector<int> bit0(currentBitString);
vector<int> bit1(currentBitString);
bit0[i] = 0;
bit1[i] = 1;
// Generate next bit strings
generateBitString(i + 1, n, bit0, allBitStrings);
generateBitString(i + 1, n, bit1, allBitStrings);
}
};