Skip to content

Files

Latest commit

984e2ad · Dec 18, 2022

History

History
24 lines (18 loc) · 523 Bytes

p0046M-permute.md

File metadata and controls

24 lines (18 loc) · 523 Bytes

Problem: 46. 全排列 ⭐⭐⭐

[TOC]

思路

回溯算法,超经典

Code

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