-
Notifications
You must be signed in to change notification settings - Fork 0
/
Subsets.cpp
48 lines (39 loc) · 1.07 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
//
// Subsets.cpp
//
//
// Created by 廷芳 杜 on 7/10/13.
// Copyright (c) 2013 __MyCompanyName__. All rights reserved.
//
#include <iostream>
class Solution {
public:
vector< vector<int> > pool;
void findAllSubsets( vector<int> &S, vector<int> &subset, int id )
{
if( id == S.size() )
{
pool.push_back( subset );
return;
}
//add this number
subset.push_back( S[id] );
findAllSubsets( S, subset, id+1 );
subset.pop_back();
//do not add this number
findAllSubsets( S, subset, id+1 );
return;
}
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
pool.clear();
if( S.size() == 0 ) return pool;
//subset must be in non-descent order.
sort( S.begin(), S.end() );
int id = 0;
vector< int > subset;
findAllSubsets( S, subset, id );
return pool;
}
};