nums = [1, 2, 3] # 回溯算法,复杂度较高,因为回溯算法就是暴力穷举,遍历整颗决策树是不可避免的 res = [] def backtrack(path=None, selects=None): if path is None: path = [] # 用来存放符合条件的结果 if not selects: res.append(path[:]) # 此时说明找到了一组 return for i in range(len(selects)): path.append(selects[i]) backtrack(path, selects[:i] + selects[i + 1:]) # 递归 path.pop() # 回溯 backtrack(selects=nums) print(res)