class Solution:
def permuter(self,nums:list[int]) -> list[list[int]]:
if not nums:
return []
def dfs(res,path,deep,visited):
if deep == len(nums):
res.append(path[:])
for i in range(len(nums)):
if visited[i] == False:
path.append(nums[i])
visited[i] = True
dfs(res,path,deep + 1,visited)
path.pop()
visited[i] = False
res = []
path = []
visited = [False] * len(nums)
dfs(res,path,0,visited)
return res
nums = list(map(int,input().split(',')))
s = Solution()
print(s.permuter(nums))
手撕9_回溯_全排列
于 2024-04-14 23:01:57 首次发布