solution.md 4.4 KB
Newer Older
1
# 子集 II
2 3
<p>给你一个整数数组 <code>nums</code> ,其中可能包含重复元素,请你返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。返回的解集中,子集可以按 <strong>任意顺序</strong> 排列。</p><div class="original__bRMd"><div><p> </p><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> </p><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>以下错误的选项是?</p>
4 5 6
## aop
### before
```cpp
每日一练社区's avatar
每日一练社区 已提交
7 8
#include <bits/stdc++.h>
using namespace std;
9 10 11
```
### after
```cpp
每日一练社区's avatar
每日一练社区 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25
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;
}
26 27 28 29
```

## 答案
```cpp
每日一练社区's avatar
每日一练社区 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
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);
        }
49

每日一练社区's avatar
每日一练社区 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
        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();
            }
        }
    }
};
68 69 70 71 72
```
## 选项

### A
```cpp
每日一练社区's avatar
每日一练社区 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
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;
    }
88

每日一练社区's avatar
每日一练社区 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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;
    }
};
108 109 110 111
```

### B
```cpp
每日一练社区's avatar
每日一练社区 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
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;
    }
};
140 141 142 143
```

### C
```cpp
每日一练社区's avatar
每日一练社区 已提交
144 145 146 147 148
class Solution
{
public:
    vector<vector<int>> subsetsWithDup(vector<int> &nums)
    {
149

每日一练社区's avatar
每日一练社区 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        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);
    }
};
175
```