连续子数组的最大和

class Solution:
    def maxSubArray(self, nums: List[int]) -> int:
        # 设当前的和为 cur_sum
        cur_sum = 0
        res = nums[0]
        for num in nums:
            # 如果当前和大于0
            if cur_sum > 0:
                cur_sum += num
            else:
                cur_sum = num
            
            res = max(res, cur_sum)
        return res

posted @ 2021-03-08 17:38  KbMan  阅读(32)  评论(0编辑  收藏  举报