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], [] ]分析:求集合的所有子集問題。題目要求子集中元素非遞減序排列,因此我們先要對原來的集合進行排序。原集合中每一個元素在子集中有兩種狀態:要麼存在、要麼不存在。這樣構造子集的過程中每個元素就有兩種選擇方法:選擇、不選擇,因此可以構造一顆二叉樹,例如對於例子中給的集合[1,2,3],構造的二叉樹如下(左子樹表示選擇該層處理的元素,右子樹不選擇),最後得到的葉子節點就是子集:
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res;
if(S.size() == 0)
return res;
vector<int> tmp;
sort(S.begin(),S.end());
find(res,S,tmp,0);
return res;
}
void find(vector<vector<int> >& res, vector<int>& S,vector<int>& tmp,int k) {
if(S.size() == k) {
res.push_back(tmp);
//return;
} else {
find(res,S,tmp,k+1);
tmp.push_back(S[k]);
find(res,S,tmp,k+1);
tmp.pop_back(); // tmp爲引用類型時要pop
}
}
};
或者用排列組合的方法:
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res;
if(S.size() == 0)
return res;
vector<int> tmp;
sort(S.begin(),S.end());
for(int k=1;k<=S.size();k++) {
tmp.clear();
find(res,S,tmp,k,0);
}
tmp.clear();
res.push_back(tmp);
return res;
}
void find(vector<vector<int> >& res, vector<int>& S,vector<int>& tmp,int k,int start) {
if(tmp.size() == k) {
res.push_back(tmp);
return;
}
for(int i= start;i<S.size();i++) {
tmp.push_back(S[i]);
find(res,S,tmp,k,i+1);
tmp.pop_back();
}
}
};