-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path78. Subsets.cpp
More file actions
50 lines (48 loc) · 1.16 KB
/
78. Subsets.cpp
File metadata and controls
50 lines (48 loc) · 1.16 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
class Solution {
vector<vector<int>> ans;
vector<int> res;
vector<int> nums;
int n;
public:
void f(int index, int lastUsed) {
if(index == -1) {
ans.push_back(res);
return;
}
for(int i = lastUsed + 1; i < n; i ++) {
int temp = res[index];
res[index] = nums[i];
f(index - 1, i);
res[index] = temp;
}
}
vector<vector<int>> subsets(vector<int>& nums) {
n = nums.size();
this->nums = nums;
ans.push_back({});
for(int i = 1; i <= n; i ++) {
res.clear();
res.assign(i, 0);
f(i - 1, -1);
}
return ans;
}
};
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int n = nums.size();
vector<vector<int>> ans;
vector<int> temp;
for(int i = 0; i < (1<<n); i ++) {
for(int j = 0; j < n; j ++) {
if(((1 << j) & i)) {
temp.push_back(nums[j]);
}
}
ans.push_back(temp);
temp.clear();
}
return ans;
}
};