class Solution {
public:
vector<vector<int> > subsets(vector<int> &S) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
sort(S.begin(), S.end());
vector<vector<int> > sets;
vector<int> set;
for (int i = 0; i <= S.size(); ++i)
{
//将所有i元子集置入子集集合中
dfs(0, i, S.size(), 0, set, sets, S);
set.push_back(0);
}
return sets;
}
void dfs(int dep, int maxDep, int n , int start, vector<int> &set,
vector<vector<int> > &sets, vector<int> &S)
{
if (dep == maxDep)
{
sets.push_back(set);
}
else
{
for (int i = start; i < n; ++i)
{
set[dep] = S[i];
dfs(dep + 1, maxDep, n, i + 1, set, sets, S);
}
}
}
};
LeetCode-Subsets
最新推荐文章于 2024-08-19 11:12:00 发布