53.最大子序和
思路:用两个值分别记录当前一直连续加过来的最大值,还有一个记录整个数组的最大值,并适时更新他们得值
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
#初始化最大值,和当前值
maxsum = nums[0]
privousum = nums[0]
for i in range(1, len(nums)):
privousum = max(privousum + nums[i], nums[i])
maxsum = max(maxsum, privousum)
return maxsum