# 子集 II

<p>给你一个整数数组 <code>nums</code> ，其中可能包含重复元素，请你返回该数组所有可能的子集（幂集）。</p><p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中，子集可以按 <strong>任意顺序</strong> 排列。</p><div class="original__bRMd"><div><p><strong>示例 1：</strong></p><pre><strong>输入：</strong>nums = [1,2,2]<strong><br />输出：</strong>[[],[1],[1,2],[1,2,2],[2],[2,2]]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>nums = [0]<strong><br />输出：</strong>[[],[0]]</pre><p><strong>提示：</strong></p><ul>	<li><code>1 <= nums.length <= 10</code></li>	<li><code>-10 <= nums[i] <= 10</code></li></ul></div></div>
<p>以下<span style="color:red">错误</span>的选项是？</p>

## aop

### before

```c
#include <bits/stdc++.h>
using namespace std;
```

### after

```c
int main()
{
    Solution sol;
    vector<vector<int>> res;
    vector<int> nums = {1, 2, 2};
    res = sol.subsetsWithDup(nums);
    for (auto i : res)
    {
        for (auto j : i)
            cout << j << " ";
        cout << endl;
    }
    return 0;
}
```

## 答案

```c
class Solution
{
public:
    vector<vector<int>> result;
    vector<vector<int>> subsetsWithDup(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        vector<int> temp;
        result.push_back(temp);
        getAns(0, nums, result, temp);
        return result;
    }
    void getAns(int start, vector<int> &nums, vector<vector<int>> &result, vector<int> temp)
    {
        if (start == nums.size() - 1)
        {
            temp.push_back(nums[start]);
            result.push_back(temp);
        }

        else
        {
            for (int i = start; i < nums.size(); i++)
            {
                while (i != 0 && i != start && nums[i] == nums[i - 1])
                {
                    i++;
                }
                if (i == nums.size())
                    break;
                temp.push_back(nums[i]);
                result.push_back(temp);
                getAns(i, nums, result, temp);
                temp.pop_back();
            }
        }
    }
};
```
## 选项


### A

```c
class Solution
{
public:
    vector<vector<int>> subsetsWithDup(vector<int> &nums)
    {

        sort(nums.begin(), nums.end());
        vector<vector<int>> res;
        vector<int> cur;
        for (int i = 0; i <= nums.size(); i++)
        {
            dfs(res, cur, nums, 0, i);
        }
        return res;
    }

    void dfs(vector<vector<int>> &res, vector<int> &cur, vector<int> &nums, int begin, int n)
    {
        if (cur.size() == n)
        {
            res.push_back(cur);
            return;
        }
        for (int i = begin; i < nums.size(); i++)
        {

            if (i > begin && nums[i] == nums[i - 1])
                continue;
            cur.push_back(nums[i]);
            dfs(res, cur, nums, i + 1, n);
            cur.pop_back();
        }
        return;
    }
};
```

### B

```c
class Solution
{
public:
    vector<vector<int>> ans;
    vector<int> cur;
    vector<int> v;
    void dfs(int depth)
    {
        ans.push_back(cur);
        if (depth == v.size())
            return;
        for (int i = depth; i < v.size(); ++i)
        {
            if (i > depth && v[i] == v[i - 1])
                continue;
            cur.push_back(v[i]);
            dfs(i + 1);
            cur.pop_back();
        }
    }
    vector<vector<int>> subsetsWithDup(vector<int> &nums)
    {
        sort(nums.begin(), nums.end());
        v = nums;
        dfs(0);
        return ans;
    }
};
```

### C

```c
class Solution
{
public:
    vector<vector<int>> subsetsWithDup(vector<int> &nums)
    {

        vector<vector<int>> result;
        vector<int> item;
        set<vector<int>> rset;
        result.push_back(item);
        sort(nums.begin(), nums.end());
        CreatSet(0, result, item, nums, rset);
        return result;
    }
    void CreatSet(int i, vector<vector<int>> &result,
                  vector<int> &item, vector<int> &nums,
                  set<vector<int>> &rset)
    {
        if (i >= nums.size())
            return;
        item.push_back(nums[i]);
        if (rset.find(item) == rset.end())
        {
            rset.insert(item);
            result.push_back(item);
        }
        CreatSet(i + 1, result, item, nums, rset);
        item.pop_back();
        CreatSet(i + 1, result, item, nums, rset);
    }
};
```
