# Subsets II(dfs)

Given a collection of integers that might contain duplicates,***nums***, return all possible subsets (the power set).

**Note:**&#x54;he solution set must not contain duplicate subsets.

**Example:**

```
Input:
 [1,2,2]

Output:

[
  [2],
  [1],
  [1,2,2],
  [2,2],
  [1,2],
  []
]
```

这里subset带重复元素问题，一定记得排序。然后 i!=pos and nums\[i]==nums\[i-1]: continue

**python dfs:**

两种方法弹入弹出path

```
path.append(nums[i])
self.dfs(nums,i+1,path,ret)
path.pop()

最后ret要copy path:     ret.append(list(path)) 或者 ret.append(path[:)
```

或者

```
self.dfs(nums,i+1,path+[nums[i]],ret)

最后直接 ret.append(path)
```

方法1

```
class Solution:
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        ret = []
        nums.sort()
        self.dfs(nums,0,[],ret)

        return ret #list(set(ret))

    def dfs(self,nums, pos,path,ret):
        ret.append(list(path))

        for i in range(pos,len(nums)):
            if i!=pos and nums[i]==nums[i-1]:
                continue
            path.append(nums[i])
            self.dfs(nums,i+1,path,ret)
            path.pop()
```

方法2：

```
class Solution:
    def subsetsWithDup(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        ret = []
        nums.sort()
        self.dfs(nums,0,[],ret)

        return ret #list(set(ret))

    def dfs(self,nums, pos,path,ret):
        ret.append(path)

        for i in range(pos,len(nums)):
            if i!=pos and nums[i]==nums[i-1]:
                continue            
            self.dfs(nums,i+1,path+[nums[i]],ret)
```


---

# 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://nataliekung.gitbook.io/ladder_code/facebook/subsets-iidfs.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.
