# 78. Subsets

## 1.問題&#x20;

* 給一組不重複整數, 回傳所有的子集合

![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LHwTlnrkPA1jJbFoHQO%2F-LHwcWhB_h_7LiYBE_R4%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-21%20%E4%B8%8B%E5%8D%887.37.49.png?alt=media\&token=2f542f9c-0f09-4b93-9007-98a3f6bf25eb)

## 2.想法&#x20;

* Recursive + back tracking

## 3.程式碼

```
class Solution {
public:
    vector<vector<int>> subsets(vector<int>& nums) {
        vector<vector<int>> subsets;
        vector<int> record;
        for (int i = 0; i < nums.size(); i++) {
            getSubSet(subsets, record, nums, 0, i + 1);
        }
        subsets.push_back(vector<int>());
        return subsets;
    }
private:
    void getSubSet(vector<vector<int>>& subsets, vector<int>& record, vector<int>& nums, int start, int numMember){
        if (numMember == record.size()) {
            subsets.push_back(record);
            return;
        }
        for (int i = start; i < nums.size(); i++) {
            record.push_back(nums[i]);
            getSubSet(subsets, record, nums, i + 1, numMember);
            record.pop_back();
        }
    }
};
```

## 4.Performance



![](https://901207480-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LGKoChvN9am4__HCIRK%2F-LHwTlnrkPA1jJbFoHQO%2F-LHwcLJElNI1MhWSu0tp%2F%E8%9E%A2%E5%B9%95%E5%BF%AB%E7%85%A7%202018-07-21%20%E4%B8%8B%E5%8D%887.35.08.png?alt=media\&token=4e452a6e-0dcc-48de-9481-3021b2fce110)
