Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

class Solution(object):
    def maxSubArray(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        max_sum = nums[0]
        sum_ = 0
        for index, val in enumerate(nums[:]):
            sum_ = val + sum_
            if sum_ > max_sum:
                max_sum = sum_
                endfix=index
            if sum_ < 0:
                sum_ = 0
                prefix=index+1
        return max_sum,prefix,endfix
x=Solution()
print(x.maxSubArray([-2,1,-3,4,5,-1,2,1,-5,4]))

输出:

(11, 3, 7)

 

 

 

 

相关文章:

  • 2021-09-16
  • 2021-12-24
  • 2021-05-16
  • 2022-12-23
  • 2022-02-05
猜你喜欢
  • 2021-08-31
  • 2021-07-08
  • 2021-08-25
  • 2021-05-21
  • 2021-10-21
  • 2021-06-30
  • 2021-09-13
相关资源
相似解决方案