python实现字符串的全排列

python没有指针操作, 修改字符串需改为list类型处理

def permutation(s,i):
    if i == len(s):
        print(s)
    else:
        for j in range(i,len(s)):
            s[j],s[i] = s[i],s[j]
            permutation(s,i+1)
            s[j],s[i] = s[i],s[j]
s = ["a","b","c"]
permutation(s,0)
['a', 'b', 'c']
['a', 'c', 'b']
['b', 'a', 'c']
['b', 'c', 'a']
['c', 'b', 'a']
['c', 'a', 'b']

猜你喜欢

转载自blog.csdn.net/m0_37422289/article/details/80570726
今日推荐