题目:

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

示例:

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

思路:第一想法就是用递归去做,用例题来说,先固定第一位分别为1,2,3。之后分别确定第二位以及第三位(文字可能表示的不是很明白,画个图更好理解一些)

萌新练习写代码的每日一练:全排列

代码:

class Solution:

    def permute(self, nums: List[int]) -> List[List[int]]:

        res = []

        

        def dfs(nums, temp):

            length = len(nums)

            if not nums:

                res.append(temp)

                return 0

            for i in range(length):

                dfs(nums[:i] + nums[i+1:], temp+[nums[i]])

        dfs(nums, [])

        return res

题目来自于LeetCode第46题

相关文章:

  • 2022-01-19
  • 2021-09-22
  • 2021-07-11
  • 2021-05-14
  • 2021-10-18
猜你喜欢
  • 2021-10-24
  • 2021-10-29
  • 2021-09-16
  • 2021-05-13
  • 2021-07-10
  • 2021-05-31
  • 2022-03-02
相关资源
相似解决方案