Find the contiguous(连续的) subarray within an array (containing at least one number) which has the largest sum.

For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.

 

 1 class Solution(object):
 2     def maxSubArray(self, A):
 3         """
 4         :type A: List[int]
 5         :rtype: int
 6         """
 7         if not A:
 8             return 0
 9 
10         curSum = maxSum = A[0]
11         for num in A[1:]:
12             curSum = max(num, curSum + num)
13             maxSum = max(maxSum, curSum)
14 
15         return maxSum

 

posted on 2017-03-14 14:52  Ci_pea  阅读(127)  评论(0)    收藏  举报