1. problem description
Given a set of distinct integers, nums, return all possible subsets (the power set).
给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。
Note: The solution set must not contain duplicate subsets.
eg:
Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
2. solution
class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
vector<vector<int> > res(1, vector<int>());
sort(S.begin(), S.end());
for (int i = 0; i < S.size(); i++) {
int n = res.size();
for (int j = 0; j < n; j++) {
res.push_back(res[j]);
res.back().push_back(S[i]);
}
}
return res;
}
};