class Solution {
public:
vector<vector<int>> all;
vector<vector<int>> subsets(vector<int>& nums) {
all.push_back(vector<int>());
if(nums.empty()) return all;
for(int i=1;i<=nums.size();i++)
{
vector<int> one;
com(nums,i,one);
}
return all;
}
void com(vector<int> & num,int k,vector<int> & one){
if(k==0)
{
all.push_back(one);
return;
}
int id=0;
while(id<num.size())
{
one.push_back(num[id]);
vector<int> tmp(num.begin()+id+1,num.end());
com(tmp,k-1,one);
one.pop_back();
id++;
}
return;
}
};
与77题很相似,就是循环调用一下即可,k从1到数组长度。