# Given a collection of distinct integers, return all possible permutations.
"""
给定一组不同的整数,返回所有可能的排列。
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(object):
def permute(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
if len(nums) <= 1:
return [nums]
ans = []
for i, num in enumerate(nums):
rest_num = nums[:i] + nums[i+1:]
for j in self.permute(rest_num):
ans.append([num] + j)
return ans
python leetcode 46. Permutations
最新推荐文章于 2020-08-04 06:43:43 发布