LeetCode46 全排列
递归求解
class Solution:
def permute(self, nums: List[int]) -> List[List[int]]:
ans, l = [], len(nums)
def dfs(cur):
if cur == l - 1:
ans.append(nums[:])
return
else:
for i in range(cur, l):
nums[i], nums[cur] = nums[cur], nums[i]
dfs(cur + 1)
nums[i], nums[cur] = nums[cur], nums[i]
dfs(cur = 0)
return ans