题目:输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
我只能说自己递归的学的不好。。。。。。大概能看懂吧。。。。。。。
# -*- coding:utf-8 -*-
class Solution:
def Permutation(self, ss):
# write code here
if not len(ss):
return []
if len(ss) == 1:
return list(ss)
charList = list(ss)
charList.sort()
pStr = []
for i in range(len(charList)):
if i > 0 and charList[i] == charList[i-1]:
continue
temp = self.Permutation(''.join(charList[:i])+''.join(charList[i+1:]))
for j in temp:
pStr.append(charList[i]+j)
# 返回值 其他时刻返回的temp
return pStr
if __name__ == '__main__':
abc = Solution()
w = abc.Permutation('aa')
print(w)