Leetcode 46. 全排列(Python3)

46. 全排列

给定一个没有重复数字的序列,返回其所有可能的全排列。

示例:

输入: [1,2,3]
输出:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

DFS代码:

class Solution(object):
    def permute(self, nums):
        """
        :type nums: List[int]
        :rtype: List[List[int]]
        """
        length = len(nums)
        if length == 0 or length == 1:return [nums]
        res = []
        for i in range(length):
            cr = self.permute(nums[:i]+nums[i+1:])
            for j in cr:
                res.append([nums[i]]+j)
        return res

猜你喜欢

转载自blog.csdn.net/qq_38575545/article/details/86560663