题解 | #没有重复项数字的全排列#
没有重复项数字的全排列
https://www.nowcoder.com/practice/4bcf3081067a4d028f95acee3ddcd2b1
class Solution: def permute(self,nums): result = [] # 回溯函数,path 是当前构建的排列,used 是标记已使用的数字 def backtrack(path, used): if len(path) == len(nums): result.append(path[:]) # 找到一个完整的排列 return for i in range(len(nums)): if not used[i]: # 做选择 path.append(nums[i]) used[i] = True # 递归构造剩下的排列 backtrack(path, used) # 撤销选择 path.pop() used[i] = False used = [False] * len(nums) backtrack([], used) return result