Answer

问题及解答

maxSubSum4.py

Posted by haifeng on 2017-03-17 22:09:21 last update 2017-03-17 22:09:44 | Edit | Answers (0)

Example :  (书本 P43,  算法4,如果用 Python 实现)
可以将下面的代码保存为一个文件: maxSubSum4.py,  按 F5 运行.
-----------------------

# Linear-time maximum contiguous subsequence sum algorithm

a=[-2,11,-4,13,-5,-2]
thisSum=0
maxSum=0
for j in range(len(a)):
    thisSum += a[j]
    if thisSum > maxSum:
        maxSum = thisSum
    elif thisSum < 0 :
        thisSum=0

print("maxSum = ", maxSum)