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