无重复元素
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int>> res;
vector<int> emp;
res.push_back(emp);//empty set
sort(S.begin(),S.end());//non-descending order
if(S.size()==0){
return res;
}
for(vector<int>::iterator it=S.begin();it<S.end();it++){
int size=res.size();
for(int i=0;i<size;i++){
vector<int> tmp;
for(vector<int>::iterator itr=res[i].begin();itr<res[i].end();itr++){
tmp.push_back(*itr);
}
tmp.push_back(*it);//add new element
res.push_back(tmp);
}
}
return res;
}
};
参考:subset