python非递归全排列实现方法
刚刚开始学习python,当前看到了函数这一节。结合数组操作,写了个非递归的全排列生成。原理是插入法,也就是在一个有n个元素的已有排列中,后加入的元素,依次在前,中,后的每一个位置插入,生成n+1个新的全排列。因为Python切割数组或者字符串,以及合并比较方便,所以,程序会节省很多代码。
def getArrayInsertCharToStr(STR,CHAR): arr =[] s_len = len(STR) index =0 while index <= s_len: #分割字符串 arr.append(STR[:index]+CHAR+STR[index:s_len]) index = index + 1 return arr def getArrayInsertCharToArray(array,CHAR): index = 0 re_array = [] while index < len(array): re_array = re_array + getArrayInsertCharToStr(array[index],CHAR) index = index + 1 return re_array def getPermutation(STR): resultArr = [STR[0]] for item in STR[1:]: resultArr = getArrayInsertCharToArray(resultArr,item) return resultArr print(getPermutation('abc'))
相关推荐
夜斗不是神 2020-11-17
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
weiiron 2020-11-16
cakecc00 2020-11-15
JakobHu 2020-11-14
guangcheng 2020-11-13
xirongxudlut 2020-11-10
solarLan 2020-11-09
pythonxuexi 2020-11-08
文山羊 2020-11-07
susmote 2020-11-07
wuShiJingZuo 2020-11-05
Pythonjeff远 2020-11-06