# 子集

<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>

## aop

### before

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

```cpp
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;
}
```

## 答案

```cpp
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();
    }

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;
    }
};
```

## 选项


### A

```cpp
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;
    }
};
```

### B

```cpp
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++;
        }

        return output;
    }
};
```

### C

```cpp
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++)
        {

            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;
    }
};
```
