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], [] ]Array Backtracking Bit Manipulation
class Solution {
public:
vector<vector<int>> res;
//set<vector<int>> res;
void function(int start,int len,vector<int> s,vector<int> tmp)
{
if(tmp.size()==len)
res.push_back(tmp);
else{
for(int i=start;i<=s.size()-(len-tmp.size());i++)
{
vector<int> v(tmp);
v.push_back(s[i]);
function(i+1,len,s,v);
}
}
}
vector<vector<int> > subsets(vector<int> &S) {
if(S.size()<0)
return res;
else if(S.size()==0)
{
vector<int> tmp;
res.push_back(tmp);
return res;
}
else{
vector<int> tmp;
res.push_back(tmp);
sort(S.begin(),S.end());
for(int len=1;len<=S.size();len++)//控制长度
{
function(0,len,S,tmp);
}
return res;
}
}
};