- class Solution:
- def permute(self, nums: List[int]) -> List[List[int]]:
- def f(nums,path,used,res):
- if len(nums)==len(path):
- res.append(path[:]) # 将 path 的副本添加到 res 中
- return
- for i in range(len(nums)):
- if used[i]:
- continue
- used[i]=True
- path.append(nums[i])
- f(nums,path,used,res)
- used[i]=False
- path.pop()
- res=[]
- f(nums,[],[False]*len(nums),res)
- return res