LeetCode(力扣)46. 全排列Python

LeetCode46. 全排列

    • 题目链接
    • 代码

题目链接

https://leetcode.cn/problems/permutations/
LeetCode(力扣)46. 全排列Python_第1张图片

代码

class Solution:
    def backtracking(self, nums, result, path, used):
        if len(path) == len(nums):
            result.append(path[:])
        
        for i in range(len(nums)):
            if used[i]:
                continue
            path.append(nums[i])
            used[i] = True
            self.backtracking(nums, result, path, used)
            path.pop()
            used[i] = False
    def permute(self, nums: List[int]) -> List[List[int]]:
        result = []
        self.backtracking(nums, result, [], [False] * len(nums))
        return result

你可能感兴趣的:(leetcode,python,算法,职场和发展)