-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17.h
51 lines (42 loc) · 1.22 KB
/
17.h
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
class Solution {
public:
/*
* @param nums: A set of numbers
* @return: A list of lists
*/
vector<vector<int>> subsets(vector<int> &nums) {
// write your code here
// 递归版
// vector<vector<int>> v;
// vector<int> r;
// subsets(nums, r, v, 0);
// return v;
// 非递归
sort(nums.begin(), nums.end());
queue<vector<int>> q;
vector<vector<int>> r;
q.push(vector<int>());
while(!q.empty()){
auto v = q.front();
q.pop();
r.push_back(v);
for(int i = v.size(); i < nums.size(); i++){
if(v.size() == 0 || v[v.size() - 1] < nums[i]){
v.push_back(nums[i]);
q.push(v);
v.pop_back();
}
}
}
return r;
}
void subsets(vector<int> &nums, vector<int> &r, vector<vector<int>> &v,
int start){
v.push_back(r);
for(int i = start; i < nums.size(); i++){
r.push_back(nums[i]);
subsets(nums, r, v, i+1);
r.pop_back();
}
}
};