leetcode刷题python之全排列

leetcode上powcai大神还是强啊,回头再多看看回溯的题目,熟悉一下这个算法

class Solution:
    def permute(self, nums: List[int]) -> List[List[int]]:
        result = []
        # middle = []
        def backtrack(nums, middle):
            if not nums:
                result.append(middle)
                return
            
            for i in range(len(nums)):
                backtrack(nums[:i] + nums[i+1:], middle + [nums[i]])
        backtrack(nums,[])
        return result

你可能感兴趣的:(leetcode_python)