python入门 数组全排列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/artisans/article/details/88825823

res = []

def letterCasePermutation(S):    
    dfs(S, 0)
    return res

def dfs(S, i):
    if(i == len(S)):
        res.append(S)
        print(S)
    else:
        for index in range(i, len(S)) :
            S[i], S[index] = S[index], S[i]           
            dfs(S, i+1)
            S[i], S[index] = S[index], S[i]     


val = [1,2,3]
print("len  ", len(val)) 

print("----------")
letterCasePermutation(val)

len 3

[1, 2, 3]
[1, 3, 2]
[2, 1, 3]
[2, 3, 1]
[3, 2, 1]
[3, 1, 2]

猜你喜欢

转载自blog.csdn.net/artisans/article/details/88825823
今日推荐