46 全排列
给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [1,2,3]
输出:
[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]
解法
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
length = len(nums)
res = [None for _ in range(length)]
total = []
def test(nums,index,res):
if index == length:
total.append(res.copy())
return
for i in range(len(nums)):
res[index] = nums[i]
test(nums[:i]+nums[i+1:],index+1,res)
test(nums,0,res)
return total