【題解】LeetCode 53. Maximum Subarray

【題目敘述】https://leetcode.com/problems/maximum-subarray/

class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        total = 0
        maxi = nums[0]
        for num in nums:
            total += num
            if total > maxi:
                maxi = total
            if total < 0:
                total = 0
        return maxi
分享本文 Share with friends