# 90. 子集 II

<https://leetcode-cn.com/problems/subsets-ii/>

## 解法一：

类似**78.子集**，由于数组存在重复，需要在其基础上加上去重条件。

类似于**40.组合总和 II**的去重判断，先排序，然后将同一层递归的重复数跳过，不同层则不需要跳过。

```python
class Solution:
    def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
        self.res = []
        tmpList = []
        nums.sort()
        for i in range(len(nums)+1):  #len+1轮循环，每次输出长度为i的所有组合
            self.backtrack(nums, tmpList, i, 0)
        return self.res

    def backtrack(self, nums, tmpList, k, start):
        if len(tmpList) == k:
            self.res.append(list(tmpList))
        for i in range(start, len(nums)):    #tmp的修改直接写在递归里
            if i-1 >= start and nums[i-1] == nums[i]:    #跳过同一层递归的重复数字
                continue
            self.backtrack(nums, tmpList+[nums[i]], k, i + 1)
```


---

# 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://cai-sen-se.gitbook.io/leetcode/1-200/90.-zi-ji-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.
