最大连续子序列和

题目就不再描述了。

最大连续子序列和问题是个很老的面试题了,最佳的解法是O(N)复杂度。最近再次遇到的时候却不能很清楚地写出来,所以在此记录一下。

def FindGreatestSumOfSubArray(self, array):
    res = max(array)
    currentSum = 0
    for i in array:
        if currentSum < 0:
            currentSum = i
        else:
            currentSum += i
        if currentSum > res:
            res = currentSum
    return res

 

posted on 2017-09-26 23:04  Peyton_Li  阅读(133)  评论(0编辑  收藏  举报

导航