class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
if not nums:
return []
#结果
res = []
#路径
path = []
#取过元素标记数组
visited = [False] * len(nums)
def dfs(res,path,deep,visited):
if len(nums) == deep:
res.append(path[:])
return
#递归场景,横向遍历,纵向递归取数
for i in range(len(nums)):
#如果数没有用过,取出来
if visited[i] == False:
path.append(nums[i])
visited[i] = True
#递归现场
dfs(res,path,deep+1,visited)
#回溯现场,回到上一层,这一层的数可以用了,路径也可以用了
visited[i] = False
path.pop()
dfs(res,path,0,visited)
return res