使用二进制进行子集表示,速度非常快~
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
int allone = (1<<nums.size()) - 1;
int k = nums.size();
vector<vector<int>> res;
res.resize(1<<k);
for(int i = 0; i<(1 << nums.size()); ++i){
int s = allone & i;
int n = nums.size()-1;
while(s != 0){
if(s & 1){
res[i].push_back(nums[n]);
}
n--;
s >>= 1;
}
}
return res;
}
};