思路:
从第一个元素开始累加,每次累加都判断并更新当前的最大值;
当累加结果小于零时,置零,将数组完整遍历一次即可得到最大和。
class Solution:
def FindGreatestSumOfSubArray(self, array):
# write code here
sm = 0
mx = array[0]
for x in range(len(array)):
sm = sm+array[x]
if sm>mx:
mx = sm
if sm <= 0:
sm = 0
return mx