-
Notifications
You must be signed in to change notification settings - Fork 0
/
subsets.cpp
85 lines (69 loc) · 1.87 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// https://oj.leetcode.com/problems/subsets/
// Date: Nov 20, 2014
#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S)
{
vector<vector<int> > vecOfvec;
vecOfvec.push_back(vector<int>());
if (S.size() == 0)
{
return vecOfvec;
}
sort(S.begin(), S.end());
stack<vector<int> > stk; // this stores the indices not the elements of vector S.
for (int i=0; i != S.size(); ++i)
{
vector<int> vec;
vec.push_back(i);
stk.push(vec);
}
while (!stk.empty())
{
vector<int> elem = stk.top();
stk.pop();
vector<int> vec;
for (vector<int>::iterator it = elem.begin(); it != elem.end(); ++it)
{
vec.push_back(S[*it]);
}
vecOfvec.push_back(vec);
int i = elem.back();
while ((i+1) < S.size())
{
vector<int> newElem = elem;
newElem.push_back(i+1);
stk.push(newElem);
++i;
}
}
return vecOfvec;
}
};
int main(int argc, char* argv[])
{
int arr[] = {1,3,0};
vector<int> vec(arr, arr+sizeof(arr)/sizeof(arr[0]));
Solution sln;
vector<vector<int> > sub_sets = sln.subsets(vec);
cout << "size: " << sub_sets.size() << endl;
for (int i = 0; i != sub_sets.size(); ++i)
{
cout << "elem #" << i << " : ";
for (int j = 0; j != sub_sets[i].size(); ++j)
{
cout << sub_sets[i][j] << " ";
}
cout << endl;
}
return 0;
}
/*
https://oj.leetcode.com/discuss/9213/my-solution-using-bit-manipulation
Tried using bit manipulation but running time N*2^N
*/