题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
http://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
class Solution:
result = []
# 跟踪回溯
def trace(self,path,choice):
# 符合要求 加入result
if len(path) == len(choice):
self.result.append(path[:])
return
# 循环选择
for item in choice:
# 剪枝判断
if item in path: continue
path.append(item)
# 选择下一个数
self.trace(path,choice)
# 回溯
path.pop()
def permute(self , num: List[int]) -> List[List[int]]:
# 排序
num.sort()
# 回溯
self.trace([], num)
return self.result