1、题目描述
给定一个可包含重复数字的序列,返回所有不重复的全排列。
2、代码详解
加入 nums[i] == nums[i-1] 判断
nums.sort()
class Solution(object):
def permuteUnique(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
# nums 选择列表,depth深度, path 路径,used 标记数组!,res 结果
def dfs(nums, size, depth, path, used, res):
# 结束条件:nums 中的元素全都在 path 中出现
if depth == size:
res.append(path[:]) # 需要传递下path的拷贝,否则对path的修改会影响到结果
return
for i in range(size):
# used[i]==False,表示未被用过
if not used[i]:
# (新加判重)剪枝!!!
# 如果出现连续重复的字符,跳出for循环(