注意的点:
1、回溯本质就是搜索树的树枝,维护三个核心变量:path(需要恢复现场),visited(需要恢复现场),以及res
解法:回溯算法
class Solution:def permute(self, nums: List[int]) -> List[List[int]]:# 回溯path = []res = []def dfs(candidate):nonlocal res, pathif not candidate:res.append(path[:])returnfor i in range(len(candidate)):path.append(candidate[i])dfs(candidate[:i] + candidate[i+1:])path.pop()dfs(nums)return res