solution.md 4.4 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1
# 子集 II
F
fix bug  
feilong 已提交
2

每日一练社区's avatar
每日一练社区 已提交
3 4
<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>
F
fix bug  
feilong 已提交
5

每日一练社区's avatar
每日一练社区 已提交
6
## aop
F
fix bug  
feilong 已提交
7

每日一练社区's avatar
每日一练社区 已提交
8
### before
F
fix bug  
feilong 已提交
9

每日一练社区's avatar
每日一练社区 已提交
10 11 12 13 14
```cpp
#include <bits/stdc++.h>
using namespace std;
```
### after
F
fix bug  
feilong 已提交
15

每日一练社区's avatar
每日一练社区 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
```cpp
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;
}
```

## 答案
F
fix bug  
feilong 已提交
34

每日一练社区's avatar
每日一练社区 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
```cpp
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();
            }
        }
    }
};
```
## 选项

F
fix bug  
feilong 已提交
77

每日一练社区's avatar
每日一练社区 已提交
78
### A
F
fix bug  
feilong 已提交
79

每日一练社区's avatar
每日一练社区 已提交
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
```cpp
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
F
fix bug  
feilong 已提交
119

每日一练社区's avatar
每日一练社区 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
```cpp
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
F
fix bug  
feilong 已提交
152

每日一练社区's avatar
每日一练社区 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
```cpp
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);
    }
};
```