高一 python 思维练习 - 非递归全排列
使用python求解列表的全排列,L=[0,1,2,3,4],使用栈的思想和非递归的方法,从列表[0]递推到L
t=[0,1,2,3,4]
R=[]
i=0
R.append([0])
count=1
#循环条件,先计算t列表的排列数量,这里是120
while count<=120:
count=count*(t[i]+1)
i+=1
if i==len(t):
break;
x=[]
for j in range(len(R)):
x.append(R.pop())
for k in x:
for j in range(0,len(k)+1):
m=k.copy()
if j<len(k):
m.insert(j,t[i])
else:
m.append(t[i])
R.append(m)
for i in range(len(R)):
print(i,R[i])