-
Notifications
You must be signed in to change notification settings - Fork 258
/
subsets.cpp
51 lines (46 loc) · 1.23 KB
/
subsets.cpp
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
// Time: O(n * 2^n)
// Space: O(1)
class Solution {
public:
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
vector<vector<int>> subsets(vector<int> &nums) {
const int size = nums.size();
const int set_size = 1 << size;
vector<vector<int>> ans;
vector<int> v;
sort(nums.begin(), nums.end());
for (int i = 0; i < set_size; ++i) {
for (int j = 0; j < size; ++j) {
if (i & (1 << j)) {
v.emplace_back(nums[j]);
}
}
ans.emplace_back(v);
v.clear();
}
return ans;
}
};
// Time: O(n * 2^n)
// Space: O(1)
class Solution2 {
public:
/**
* @param S: A set of numbers.
* @return: A list of lists. All valid subsets.
*/
vector<vector<int> > subsets(vector<int> &nums) {
vector<vector<int>> result(1);
for (size_t i = 0; i < nums.size(); ++i) {
const size_t size = result.size();
for (size_t j = 0; j < size; ++j) {
result.emplace_back(result[j]);
result.back().emplace_back(nums[i]);
}
}
return result;
}
};