-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0078. Subsets.cpp
More file actions
43 lines (37 loc) · 964 Bytes
/
0078. Subsets.cpp
File metadata and controls
43 lines (37 loc) · 964 Bytes
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
// Task : https://leetcode.com/problems/subsets/
#include <iostream>
#include <vector>
std::vector<std::vector<int>> subsets(std::vector<int>& nums) {
int size = nums.size();
int totalSize = 1 << size;
std::vector<std::vector<int>> res;
for (int i = 0; i < totalSize; ++i) {
std::vector<int> subset;
for (int j = 0; j < size; ++j) {
if (i & (1 << j)) {
subset.push_back(nums[j]);
}
}
res.push_back(subset);
}
return res;
}
void printMatrix(std::vector<std::vector<int>> mat) {
for (size_t i = 0; i < mat.size(); i++) {
std::cout << "{ ";
for (size_t j = 0; j < mat[i].size(); j++) {
std::cout << mat[i][j] << " ";
}
std::cout << "} \n";
}
std::cout << '\n';
}
int main() {
// Example 1:
std::vector<int> vec1 = { 1, 2, 3 };
printMatrix(subsets(vec1));
// Example 2:
std::vector<int> vec2 = { 0 };
printMatrix(subsets(vec2));
return 0;
}