forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
/
subsets.cpp
36 lines (34 loc) · 995 Bytes
/
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
// Time: O(n * 2^n)
// Space: O(1)
class Solution {
public:
vector<vector<int> > subsets(vector<int> &nums) {
vector<vector<int>> result(1);
sort(nums.begin(), nums.end());
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;
}
};
// Time: O(n * 2^n)
// Space: O(1)
class Solution2 {
public:
vector<vector<int> > subsets(vector<int> &nums) {
vector<vector<int>> result(1);
sort(nums.begin(), nums.end());
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;
}
};