# 90. Subsets II

## 1.問題&#x20;

![](/files/-LNd46ug194fGiwCVYV7)

## 2.想法 <a href="#id-2-xiang-fa" id="id-2-xiang-fa"></a>

* 提問
  * 確認題意:  求數列所有的可能組合,  數列是未經排序, 而且可能有重複數字
* function header, parameter
* test input
* 說明想法
  * 做法跟原本的78.subset相同, 但是需要先排序, 而且若num\[i] == num\[i - 1]時則略過
* 測試計算複雜度

## **3.程式碼** <a href="#id-3-cheng-shi" id="id-3-cheng-shi"></a>

```
class Solution {
public:
    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> record;
        vector<bool> used(nums.size(), false);
        sort(nums.begin(), nums.end());
        for (int i = 1; i <= nums.size(); i++) {
            getSubsets(nums, res, i, 0, record, used);
        }
        res.push_back(vector<int>());
        return res;
    }
private:
    void getSubsets(vector<int>& nums, vector<vector<int>>& res, int n, int start, vector<int>& record, vector<bool>& used) {
        if (record.size() == n) {
            res.push_back(record);
            return;
        }
        
        for (int i = start; i < nums.size(); i++) {
            if (!used[i]) {
                if (i > 0 && nums[i] == nums[i - 1] && !used[i - 1]) {
                    continue;
                }
                record.push_back(nums[i]);
                used[i] = true;
                getSubsets(nums, res, n, i + 1, record, used);
                record.pop_back();
                used[i] = false;
            }
        }
    }
};
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jenhsuan.gitbook.io/algorithm/leetcode/90.-subsets-ii.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
