-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets.cpp
More file actions
66 lines (58 loc) · 1.34 KB
/
Subsets.cpp
File metadata and controls
66 lines (58 loc) · 1.34 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Source : https://oj.leetcode.com/problems/subsets/
// Author : zheng yi xiong
// Date : 2015-01-14
/**********************************************************************************
*
* 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],
* []
* ]
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > vec_vec_int;
vector<int> temp_vec;
vec_vec_int.push_back(temp_vec);
for (int i = 0; i < S.size(); ++i)
{
int vec_vec_len = vec_vec_int.size();
for (int j = 0; j < vec_vec_len; ++j)
{
vector<int> temp_vec(vec_vec_int[j]);
temp_vec.push_back(S[i]);
vec_vec_int.push_back(temp_vec);
}
}
return vec_vec_int;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> S;
S.push_back(1);
S.push_back(2);
S.push_back(3);
Solution so;
vector<vector<int> > vec_vec_int = so.subsets(S);
return 0;
}