- NC221 集合的所有子集(二)
牛客链接:NC221 集合的所有子集(二)
这里通过位运算进行实现。首先通过一个集合,用以防止存入重复vector,最终结果拷贝到vector中即可。 然后通过位运算,一个有nums.size()位。每一位置1则表示nums中该位会被选择。
class Solution {
public:
vector > subsets(vector& nums) {
// write code here
set > ans;
vector > res;
vector temp;
for(int i = 0; i
temp.clear();
for(int j =0;j
if((i>>j & 1) == 1)
temp.push_back(nums[j]);
}
sort(temp.begin(),temp.end());
ans.insert(temp);
}
for(vector i : ans)
res.push_back(i);
return res;
}
};



