题目描述
class Solution:
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
def backtrack(first=0):
# if all integers are used up
if first == n:
output.append(nums[:]) #nums[:]等于复制数组,不修改原数组
for i in range(first, n):
# place i-th integer first
# in the current permutation
nums[first], nums[i] = nums[i], nums[first]
# use next integers to complete the permutations
backtrack(first + 1)
# backtrack
nums[first], nums[i] = nums[i], nums[first] #交换完顺序再把顺序给改回来
n = len(nums)
output = []
backtrack()
return output
参考链接
https://leetcode-cn.com/problems/permutations/solution/quan-pai-lie-by-leetcode/