ans = []
def trace(path,choices):
if len(path) == len(nums):
ans.append(list(path))
for i in choices:
if i in path:
continue
path.append(i)
trace(path,choices)
path.pop()
return ans
nums = [1,2,3]
print(trace([],nums))
>>> [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]
res = []
path = []
def bt(nums,startIndex):
res.append(path[:])
for i in range(startIndex,len(nums)):
path.append(nums[i])
bt(nums,i+1)
path.pop()
nums = [1,2,3]
bt(nums,0)
print(res)
>>> [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]]