递归思想实例
简述
代码实例
1)字母反转
#递归思想,递归向里面往外扩展呈现出来的也就是从里到外的 def func(str_num): if len(str_num) <1: return str_num return func(str_num[1:])+str_num[0] result = func(str_num) print result
2)快排
def kuaiPai(num): da,zhong,xiao = [],[],[] #如果没有条件条件限制会报错“IndexError: list index out of range” #新的递归da或xiao数组赋给num; #小于1说明是最后一次递归return num而不是最后一次递归的数据 if len(num)<1: return num pivo = num[0] for i in range(len(num)): if num[i] > pivo: da.append(num[i]) elif num[i] < pivo: xiao.append(num[i]) else: zhong.append(num[i]) less = kuaiPai(xiao) more = kuaiPai(da) return less+zhong+more print(kuaiPai(num=[8,2,3,1,5,2]))
转载引用请标明出处,本博出自喝了少不如不喝的博客https://home.cnblogs.com/u/wangdadada