给定一个 没有重复 数字的序列,返回其所有可能的全排列。
示例:
输入: [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]]:
import itertools
return list(itertools.permutations(nums))
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
res=[]
def helper(nums,curr_nums):
if(not nums):
res.append(curr_nums[:])
return
for i ,v in enumerate( nums ):
curr_nums.append(v)
helper(nums[:i]+nums[i+1:],curr_nums)
curr_nums.pop()
helper(nums,[])
return res