solution.md 3.6 KB
Newer Older
1
# 子集
2 3
<p>给你一个整数数组 <code>nums</code> ,数组中的元素 <strong>互不相同</strong> 。返回该数组所有可能的子集(幂集)。</p><p>解集 <strong>不能</strong> 包含重复的子集。你可以按 <strong>任意顺序</strong> 返回解集。</p><p> </p><p><strong>示例 1:</strong></p><pre><strong>输入:</strong>nums = [1,2,3]<strong><br />输出:</strong>[[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]</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>	<li><code>nums</code> 中的所有元素 <strong>互不相同</strong></li></ul>
<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<int> nums = {1, 2, 3};
    vector<vector<int>> res;
    res = sol.subsets(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
class Solution
{
private:
    void subsetss(int numssize, int i, vector<int> &nums, vector<vector<int>> &output, vector<int> &newtemp)
    {
        if (i >= numssize)
        {
            output.push_back(newtemp);
            return;
        }
        newtemp.push_back(nums[i]);
        subsetss(numssize, i + 1, nums, output, newtemp);
        newtemp.pop_back();
    }
44

每日一练社区's avatar
每日一练社区 已提交
45 46 47 48 49 50 51 52 53 54 55
public:
    vector<vector<int>> subsets(vector<int> &nums)
    {
        int numssize = nums.size();
        int i = 0;
        vector<vector<int>> output;
        vector<int> newtemp;
        subsetss(numssize, i, nums, output, newtemp);
        return output;
    }
};
56 57 58 59 60
```
## 选项

### A
```cpp
每日一练社区's avatar
每日一练社区 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
class Solution
{
public:
    vector<vector<int>> subsets(vector<int> &nums)
    {
        int numssize = nums.size();
        int numscount = 1 << numssize;
        vector<vector<int>> output;
        int i = 0;
        while (i < numscount)
        {
            vector<int> newtemp;
            for (int x = 0; x < numssize; x++)
            {
                if ((1 << x) & i)
                {
                    newtemp.push_back(nums[x]);
                }
            }
            i++;
            output.push_back(newtemp);
        }
        return output;
    }
};
86 87 88 89
```

### B
```cpp
每日一练社区's avatar
每日一练社区 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
class Solution
{
public:
    vector<vector<int>> subsets(vector<int> &nums)
    {
        vector<vector<int>> output;
        vector<int> subset;
        int length = nums.size();
        output.push_back(subset);
        int current = 0;
        int output_size;
        while (current < length)
        {
            output_size = output.size();
            for (int i = 0; i < output_size; i++)
            {
                vector<int> newinsert = output[i];
                newinsert.insert(newinsert.end(), nums[current]);
                output.push_back(newinsert);
            }
            current++;
        }
112

每日一练社区's avatar
每日一练社区 已提交
113 114 115
        return output;
    }
};
116 117 118 119
```

### C
```cpp
每日一练社区's avatar
每日一练社区 已提交
120 121 122 123 124 125 126 127 128 129 130 131
class Solution
{
public:
    vector<vector<int>> subsets(vector<int> &nums)
    {
        vector<vector<int>> res;
        if (nums.empty())
            return res;
        res.push_back({});
        int n = nums.size();
        for (int i = 0; i < n; i++)
        {
132

每日一练社区's avatar
每日一练社区 已提交
133 134 135 136 137 138 139 140 141 142 143
            int nRes = res.size();
            for (int j = 0; j < nRes; j++)
            {
                vector<int> temp = res[j];
                temp.push_back(nums[i]);
                res.push_back(temp);
            }
        }
        return res;
    }
};
144
```