python实现求解最大连续子序列

def maxsum(list): max=list[0] for i in range(0,len(list)): //子序列开始位置 sum=list[i] for j in range(i+1,len(list)): //子序列结束位置 sum+=list[j] //子序列求和 if sum>max: //最大值保存凌驾于整个子序列循环上 max=sum s=i //保留最后一个最大值索引,便于切片 e=j return (max,list[s:e+1]) test=[-2,1,-2,3,10,-4,7,2,5,-2,1] print(maxsum(test)) 比较笨重的暴力解决方法,期待优化版。

猜你喜欢

转载自www.cnblogs.com/rookieboy/p/9717682.html