leetcode 动态规划 —— 53(最大子序列的和)

1. 连续数组最大子序列的和

  • 53. Maximum Subarray

    在这里插入图片描述

    • 小于0,就重新开始;
    • 大于0,则继续累加;
    class Solution:
        def maxSubArray(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            sums = [nums[0]]
            for i in range(1, len(nums)):
                if sums[-1] <= 0:
                    sums.append(nums[i])
                else:
                    sums.append(sums[-1] + nums[i])
            return max(sums)
    

你可能感兴趣的:(leetcode,面试)