题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则按字典序打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
解答
递归
# coding:utf-8 class Solution: def Permutation(self, ss): # write code here if len(ss) == 0: return [] if len(ss) == 1: return [ss] l1 = [] for i in range(len(ss)): head = ss[i] for j in self.Permutation(ss[0:i] + ss[i+1:]): probe = head + j if probe not in l1: l1.append(probe) return l1
p = Solution() ret = p.Permutation('abc') print ret
结束!