# 【LeetCode】78. Subsets

Given a set of distinct integers, nums, return all possible subsets (the power set).

Note: The solution set must not contain duplicate subsets.

Example:

Input: nums = [1,2,3]
Output:
[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]

class Solution {
public:
vector<vector<int>> subsets(vector<int>& nums) {

vector<vector<int>> result;

for (int i = 0; i <= nums.size(); ++ i) {
vector<bool> visit(nums.size(), false);
vector<int> current;
dfs(i, 0, nums, visit, current, result);
}

return result;
}
void dfs(int k, int index, const vector<int> &nums, vector<bool> &visit, vector<int> &current, vector<vector<int>> &result) {
if (!k) {
result.push_back(current);
return ;
}

for (int i = index; i < nums.size(); ++ i) {
if (!visit[i]) {
visit[i] = 1;
if (!current.size() || i >= current.size()) {
current.push_back(nums[i]);
dfs(k - 1, i, nums, visit, current, result);
current.pop_back();
}
visit[i] = 0;
}
}
}
};

08-04 156
06-21 1423

01-06 96
05-27 997
06-07 1万+
01-03 586