原题链接https://leetcode.com/problems/subsets-ii/
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the power set).
Note: The solution set must not contain duplicate subsets.
Example:
Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
def backtrack(path,nums):
if not nums:
return
for i, num in enumerate(nums):
if i == 0 or nums[i] != nums[i-1]:
path.append(num)
res.append(path[:])
backtrack(path,nums[i+1:])
path.pop()
nums.sort()
path = []
res = []
backtrack(path,nums)
return res + [[]]
网友评论