classSolution: defsubsetsWithDup(self, nums: List[int]) -> List[List[int]]: #方案一: # nums.sort() # path,result = [],[] # used = [False for _ in nums] # def split_tree(nums,start_index): # result.append(path.copy()) # if start_index == len(nums): # return # for i in range(start_index,len(nums)): # if i>0 and nums[i]==nums[i-1] and used[i-1]==False: # 树层方向(横向) 去重 # continue # path.append(nums[i]) # used[i] = True # split_tree(nums,i+1) # path.pop() # used[i] = False # return result # return split_tree(nums,0)
# 方案二 nums.sort() path,result = [],[] defsplit_tree(nums,start_index): result.append(path.copy()) used = set() for i inrange(start_index,len(nums)): if nums[i] in used : # 树层方向(横向) 去重 continue used.add(nums[i]) path.append(nums[i]) split_tree(nums,i+1) path.pop() return result return split_tree(nums,0)