如:
输入字符串:‘ac‘,输出:[‘ac‘,‘ca‘]
输入字符串:‘abc‘ ,输出:[‘abc‘,‘acb‘,‘bac‘,‘bca‘,‘cab‘,‘cba‘]
输入字符串:‘acc‘,输出:[‘acc‘,‘cac‘,‘cca‘]
如:‘abc‘,对于‘a‘,返回’bc‘的全排列字典序,对于‘b‘,返回‘ac‘的全排列,对于‘c‘,返回‘ab‘的全排列。【循环加递归】
代码1:
def printstr(s): liststr=[] result=[] if len(s)==1: liststr.append(s) return liststr else: for i in range(len(s)): liststr1=printstr(s[:i]+s[i+1:]) for item in liststr1: result.append(s[i]+str(item)) return list(set(result)) s=input() print(printstr(s))
代码2:
res = list() def traverse(ss,join_ss=‘‘): if ss: for i,s in enumerate(ss): sub_ss = ss[:i]+ss[i+1:] traverse(sub_ss,join_ss+s) elif join_ss and join_ss not in res: res.append(join_ss) return res result = traverse(‘abc‘,‘‘) print(result)
原文:https://www.cnblogs.com/Lee-yl/p/8964741.html