Given a set of distinct integers, S, return all possible subsets.
Note:
- Elements in a subset must be in non-descending order.
- The solution set must not contain duplicate subsets.
For example,
If S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ]
class Solution {
void foo(int *array, int len, int remainNum, vector<int> &curVector, vector<vector<int>> &retVector)
{
if (remainNum == 0)
{
retVector.push_back(curVector);
return;
}
for (int i = 0; i<len; i++)
{
curVector.push_back(array[i]);
foo(&array[i+1], len-i-1, remainNum-1, curVector, retVector);
curVector.pop_back();
}
}
public:
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector<vector<int>> retVector;
vector<int> curVector;
sort(S.begin(), S.end());
retVector.push_back(curVector);
int vectorSize = S.size();
for (int subsetNum = 1; subsetNum <= vectorSize; subsetNum++)
{
for(int i = 0; i < vectorSize; i++)
{
curVector.push_back(S[i]);
foo(&S[i+1], vectorSize-i-1, subsetNum-1, curVector, retVector);
curVector.pop_back();
}
}
return retVector;
}
};