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