leetcode46. Permutations

2020-06-22  本文已影响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
上一篇 下一篇

猜你喜欢

热点阅读