题解 | #有重复项数字的全排列#
有重复项数字的全排列
https://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param num int整型一维数组
# @return int整型二维数组
#
class Solution:
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
# write code here
ans = []
path = []
n = len(nums)
on_path = [False] * n
nums.sort()
def dfs(i: int) -> None:
if i == n:
ans.append(path.copy())
return
for j in range(0, n):
if on_path[j] or (j > 0 and nums[j] == nums[j - 1] and not on_path[j - 1]):
continue
path.append(nums[j])
on_path[j] = True
dfs(i + 1)
path.pop()
on_path[j] = False
dfs(0)
return ans
