90. 子集 II

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution:
def subsetsWithDup(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 = [],[]
def split_tree(nums,start_index):
result.append(path.copy())
used = set()
for i in range(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)