美文网首页
leetcode46. Permutations

leetcode46. Permutations

作者: 就是果味熊 | 来源:发表于2020-06-22 11:29 被阅读0次

原题地址
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

相关文章

网友评论

      本文标题:leetcode46. Permutations

      本文链接:https://www.haomeiwen.com/subject/ewlxfktx.html