leetcode 53-> Maximum Subarray
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)