子集
分析
二进制压缩
class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>>ans;
for (int status = 0; status <(1 << nums.size()); status++) {
ans.push_back(getSub(nums, status));
}
return ans;
}
vector<int>getSub(vector<int>& nums, int status) {
vector<int>ans;
for (int i = 0; i <nums.size(); i++) {
if (status & (1 << i)) {
ans.push_back(nums[i]);
}
}
return ans;
}
};