# int32bit / leetcode

Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
solve.cpp

## Subsets

Given a set of distinct integers, nums, 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 nums = `[1,2,3]`, a solution is:

``````[
[3],
[1],
[2],
[1,2,3],
[1,3],
[2,3],
[1,2],
[]
]
``````

## Solution

```vector<vector<int>> subsets_recurisive(vector<int> &nums) {
vector<vector<int>> result;
vector<int> cur;
sort(nums.begin(), nums.end());
subsets_recurisive(result, cur, nums, 0);
return result;
}
void subsets_recurisive(vector<vector<int>> &result, vector<int> cur, const vector<int> &nums, int i) {
if (i == nums.size()) {
result.push_back(cur);
return;
}
subsets_recurisive(result, cur, nums, i + 1); // i is not included
cur.push_back(nums[i]);
subsets_recurisive(result, cur, nums, i + 1); // i in included
}```

```vector<vector<int>> subsets_iterative(vector<int> &nums) {
vector<vector<int>> result(1, vector<int>());
sort(begin(nums), end(nums));
int n = nums.size();
for (int i = 0; i < n; ++i) {
int m = result.size();
for (int j = 0; j < m; ++j) {
result.push_back(result[j]); // copy, not include current number
result.back().push_back(nums[i]); // include current number
}
}
return result;
}```

## 扩展

Subsets II, 有重复元素的情况

You can’t perform that action at this time.