原题地址
https://leetcode.com/problems/permutations/submissions/
Given a collection of distinct integers, return all possible permutations.
Example:
Input: [1,2,3]
Output:
[
[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]]:
res = []
if not nums:
return [[]]
for i, num in enumerate(nums):
rest_nums = nums[:i] + nums[i+1:]
for j in self.permute(rest_nums):
res.append([num] + j)
return res
网友评论