三个数能排出几种顺序
走不通就退回来,换一条再走
贪心
回溯
✗
把 [1, 2, 3] 排成一排,一共有几种排法?运行下面这段程序:
def perm(a):
res = []
path = []
used = [False] * len(a)
def dfs():
if len(path) == len(a):
res.append(list(path))
return
for i in range(len(a)):
if used[i]:
continue
used[i] = True
path.append(a[i])
dfs()
used[i] = False
path.pop()
dfs()
return res
print(len(perm([1, 2, 3])))
全部评论