Python字符串的全排列算法实例详解
本文实例讲述了Python字符串的全排列算法。分享给大家供大家参考,具体如下:
题目描述
输入一个字符串,按字典序打印出该字符串中字符的所有排列。例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba。
输入描述
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
注意有可能重复,因此需要判断
注意list的append方法和list的+方法的区别
append方法在list后面添加元素
+方法在list后面添加list
如果使用append(list),那么list中所有的元素都会作为一项插入
swap函数将新的元素与之前的所有元素交换,返回一个列表,每一次交换都插入一个元素,因此是append方法
def swap(self, newElem, Elem):
 result = []
 listElem = list(Elem)
 listElem.insert(0, newElem)
 result.append(''.join(listElem))
 for i in range(1, len(listElem)):
 preList = listElem[:] #注意这个地方
 listElem[0], listElem[i] = listElem[i], listElem[0]
 if listElem != preList: #处理重复情况
 result.append(''.join(listElem))
 listElem[0], listElem[i] = listElem[i], listElem[0]
 return result
如果使用+方法:
def swap(newElem, Elem):
 result = []
 listElem = list(Elem)
 listElem.insert(0, newElem)
 #result.append(''.join(listElem))
 result += ''.join(listElem)
 for i in range(1, len(listElem)):
 preList = listElem[:] # 注意这个地方
 listElem[0], listElem[i] = listElem[i], listElem[0]
 if listElem != preList: # 处理重复情况
  #result.append(''.join(listElem))
  result += ''.join(listElem)
 listElem[0], listElem[i] = listElem[i], listElem[0]
 return result
print(swap('1', '234'))
>>>>['1', '2', '3', '4', '2', '1', '3', '4', '3', '2', '1', '4', '4', '2', '3', '1']
递归调用函数
这个地方要用+号,因为是加上每次调用的结果list(有多个元素),而不能append
def recurtionPermutation(self, ss, index): result = [] if index == 0: result.append(ss[0]) else: previousList = self.recurtionPermutation(ss, index - 1) newElem = ss[index] #print(previousList) for Elem in previousList: result += self.swap(newElem, Elem) #这里返回的是一个数组,数组加数组使用+,数组加元素使用append符号 return result
按照字典排序
这里我按照冒泡字典排序,实际上没有必要,比较字符大小直接可以用sorted函数。
sorted函数又方便又高效
def BubbleSortByDic(self, result): for i in range(len(result)): for j in range(len(result) - 1, i, -1): if result[j] < result[i]: result[i], result[j] = result[j], result[i] return result
AC代码:
class Solution:
 def swap(self, newElem, Elem):
 result = []
 listElem = list(Elem)
 listElem.insert(0, newElem)
 result.append(''.join(listElem))
 for i in range(1, len(listElem)):
  preList = listElem[:] #注意这个地方
  listElem[0], listElem[i] = listElem[i], listElem[0]
  if listElem != preList: #处理重复情况
  result.append(''.join(listElem))
  listElem[0], listElem[i] = listElem[i], listElem[0]
 return result
 def recurtionPermutation(self, ss, index):
 result = []
 if index == 0:
  result.append(ss[0])
 else:
  previousList = self.recurtionPermutation(ss, index - 1)
  newElem = ss[index]
  #print(previousList)
  for Elem in previousList:
  result += self.swap(newElem, Elem) #这里返回的是一个数组,数组加数组使用+,数组加元素使用append符号
 return result
 # def BubbleSortByDic(self, result):
 # for i in range(len(result)):
 #  for j in range(len(result) - 1, i, -1):
 #  if result[j] < result[i]:
 #   result[i], result[j] = result[j], result[i]
 # return result
 def Permutation(self, ss):
 # write code here
 if ss == '':
  return []
 #return self.BubbleSortByDic(self.recurtionPermutation(ss, len(ss) - 1))
 return sorted(self.recurtionPermutation(ss, len(ss) - 1))
print(Solution().Permutation('acdfb'))
更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数学运算技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。
