题解 | #有重复项数字的全排列#
有重复项数字的全排列
https://www.nowcoder.com/practice/a43a2b986ef34843ac4fdd9159b69863
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param num int整型一维数组 # @return int整型二维数组 # class Solution: def permuteUnique(self, num: list[int]) -> list[list[int]]: # write code here ans = [] length = len(num) visited = [False for i in range(length)] L = [] num.sort() print(num) def traversal(num, visited, L): # print("start") if len(L) == length: ans.append(L.copy()) return for i in range(length): #在这里进行树枝标记 if visited[i]: continue #在这里进行树层剪枝 if i > 0 and num[i] == num[i - 1] and visited[i - 1] == False: continue L.append(num[i]) visited[i] = True traversal(num, visited, L) #回溯 L.pop() visited[i] = False traversal(num, visited, L) return ans