class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int N=S.size();
sort(S.begin(),S.end());
vector<vector<int> > ans;
vector<int> tmp;
bool exi;
//原集合中有N个元素,那么一共有2^N个子集,可以用一个整数i从0遍历到2^N -1,转成二进制,
//i的每一位代表当前元素在不在结果中,0代表空集,11...1代表原集合
for(int i=0;i<pow(2,N);i++)//
{
tmp.clear();
for(int j=0;j<N;j++)
{
exi=(i>>j)&1;
if(exi)
tmp.push_back(S[j]);
}
ans.push_back(tmp);
}
return ans;
}
};
class Solution {
vector<vector<int> > result;
public:
void sub(vector<int> &S, vector<int> tmp, int level){
if(level == S.size()){
result.push_back(tmp);
return;
}
sub(S, tmp, level + 1);//当前元素不在集合中
tmp.push_back(S[level]);//当前元素在集合中
sub(S, tmp, level + 1);
}
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//求一个集合的全部子集,和全排列类似,第一反应就是DFS,
//一个二叉树,左边是当前元素不在集合中,右边是当前元素在集合中
int N = S.size();
sort(S.begin(), S.end());
result.clear();
vector<int> tmp;
sub(S, tmp, 0);
return result;
}
};