LeetCode46 全排列

LeetCode46 全排列

递归求解

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

        ans, l = [], len(nums)

        def dfs(cur):
            if cur == l - 1:
                ans.append(nums[:])
                return
            else:
                for i in range(cur, l):
                    nums[i], nums[cur] = nums[cur], nums[i]
                    dfs(cur + 1)
                    nums[i], nums[cur] = nums[cur], nums[i]

        dfs(cur = 0)
        return ans

posted on 2022-06-30 15:03  solvit  阅读(18)  评论(0编辑  收藏  举报

导航