[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}(代码片
Posted chiyeung
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}(代码片相关的知识,希望对你有一定的参考价值。
def permutationN(n): a=[None]*n for i in range(n): a[i]=i+1 sum=1 for j in range(n): sum*=(j+1) i=0 for k in range(sum): # If you want to use stack #a[i:i+2]=swapStack(a[i],a[i+1]) a[i:i+2]=[a[i+1],a[i]] print(a) i+=1 if i==(n-1): i=0
以上是关于[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}(代码片的主要内容,如果未能解决你的问题,请参考以下文章
[Algorithm] A nonrecursive algorithm for enumerating all permutations of the numbers {1,2,...,n}(代码片