根据python文档:
Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.
可见python中itertool库中permutations()函数是根据初始参数存放次序决定的,而不是根据值的大小,
而c++的stl库中的next_permutation()函数是根据值的大小全排列.如果初始参数有序,则两种函数结果相同,反之不同
使用python重新写permutations函数,使之与stl中函数功能相同
def next_permutation(n):
flag=False
for i in range(len(n)-2,-1,-1):
if n[i]<n[i+1]:
flag=True
break
if not flag:
return False
for j in range(len(n)-1,i,-1):
if n[j]>n[i]:
break
n[i],n[j]=n[j],n[i]
n[i+1:]=n[:i:-1]
return True