forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subsets.cpp
65 lines (61 loc) · 1.31 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 24, 2012
Problem: Subsets
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
Given a set of distinct integers, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3], a solution is:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
Solution:
There is another good solution provided by
https://github.com/anson627/leetcode/blob/master/Subsets/src/Subsets.cpp
.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
using namespace std;
class Solution {
public:
vector<vector<int> > result;
void recursion(int dep, int max_dep, int s, vector<int> &subset,
vector<int> &S) {
if (dep == max_dep) {
result.push_back(subset);
}
for (unsigned i = s; i < S.size(); i++) {
subset.push_back(S[i]);
recursion(dep + 1, max_dep, i + 1, subset, S);
subset.pop_back();
}
}
vector<vector<int> > subsets(vector<int> &S) {
result.clear();
sort(S.begin(), S.end());
for (unsigned i = 0; i <= S.size(); i++) {
vector<int> subset;
recursion(0, i, 0, subset, S);
}
return result;
}
};