from typing import List class Solution: def permute(self, nums: List[int]) -> List[List[int]]: def dfs(nums, size, depth, path, used, res): if depth == size: res.append(path[:]) return for i in range(size): # used[i] 为 false时 if not used[i]: used[i] = True path.append(nums[i]) def dfs(nums, size, depth, path, used, res): if depth == size: res.append(path[:]) return for i in range(size): # used[i] 为 false时 if not used[i]: used[i] = True path.append(nums[i]) dfs(nums, size, depth + 1, path, used, res) used[i] = False path.pop() used[i] = False path.pop() size = len(nums) if len(nums) == 0: return [] # 一维数组赋值 used = [False for _ in range(size)] # 二维数组赋值 # [[False for _ in range(size)] for _ in range(size)] res = [] dfs(nums, size, 0, [], used, res) return res if __name__ == '__main__': nums = [1, 2, 3] solution = Solution() res = solution.permute(nums) print(res)