topic:
Topic links: https://leetcode-cn.com/problems/subsets-ii/
Problem-solving ideas:
"All possible" = + prune back
To consider not contain duplicate subset, so when traversing, as if an element has traversed the first character, you need to skip the same elements, use the next non-repeating elements continue backtracking
Code:
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
def create_res(start, nums, res, path):
if start > len(nums):
return
for index in range(start, len(nums)):
if index > start and nums[index] == nums[index - 1]:
continue
path.append(nums[index])
res.append(path[:])
create_res(index + 1, nums, res, path)
path.pop()
nums.sort()
res = [[]]
create_res(0, nums, res, [])
return res