-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path47.全排列Ⅱ.py
42 lines (38 loc) · 1.31 KB
/
47.全排列Ⅱ.py
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
37
38
39
40
41
42
# 给定一个可包含重复数字的序列,返回所有不重复的全排列。
#
# 示例:
#
# 输入: [1,1,2]
# 输出:
# [
# [1,1,2],
# [1,2,1],
# [2,1,1]
# ]
class Solution:
def permuteUnique(self, nums):
if len(nums) == 0:
return []
# 修改 1:首先排序,之后才有可能发现重复分支,升序、倒序均可
nums.sort()
# nums.sort(reverse=True)
used = [False] * len(nums)
res = []
self.__dfs(nums, 0, [], used, res)
return res
def __dfs(self, nums, index, pre, used, res):
if index == len(nums):
res.append(pre.copy())
return
for i in range(len(nums)):
if not used[i]:
# 修改 2:因为排序以后重复的数一定不会出现在开始,故 i > 0
# 和之前的数相等,并且之前的数还未使用过,只有出现这种情况,才会出现相同分支
# 这种情况跳过即可
if i > 0 and nums[i] == nums[i - 1] and not used[i - 1]:
continue
used[i] = True
pre.append(nums[i])
self.__dfs(nums, index + 1, pre, used, res)
used[i] = False
pre.pop()