class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {
vector<vector<int>> result(1);
for(int i = 0; i < nums.size(); i++){
int len = result.size();//一定要在循环外面确定result的大小,在循环中result的大小是变化的
for(int j=0; j < len; j++){
result.push_back(result[j]);//复制之前result中的所有元素
result.back().push_back(nums[i]); //在复制的项后面添加nums[i]
}
}
return result;
}
};
起始subset集为:[]
添加S0后为:[], [S0]
添加S1后为:[], [S0], [S1], [S0, S1]
添加S2后为:[], [S0], [S1], [S0, S1], [S2], [S0, S2], [S1, S2], [S0, S1, S2]