# LeetCode53. 最大子序和 （动态规划/分治法）

class Solution(object):
def maxSubArray(self, nums):
result = nums[:]
maxSum = nums[0]
for i in range(1,len(nums)):
if result[i-1]>0:
result[i] = nums[i] + result[i-1]
if result[i]>maxSum:
maxSum = result[i]
return maxSum

class Solution(object):
def maxSubArray(self, nums):
left = 0
right = len(nums)-1
maxSum = self.divide(nums,left,right)
return maxSum

def divide(self,nums,left,right):
if left==right:
return nums[left]
center = (left+right)/2
leftMaxSum = self.divide(nums,left,center)
rightMaxSum = self.divide(nums,center+1,right)
leftBorderSum = nums[center]
leftSum = nums[center]
for i in range(center-1,left-1,-1):
leftSum += nums[i]
if leftSum>leftBorderSum:
leftBorderSum = leftSum
rightBorderSum = nums[center+1]
rightSum = nums[center+1]
for i in range(center+2,right+1):
rightSum += nums[i]
if rightSum>rightBorderSum:
rightBorderSum = rightSum
BorderSum = leftBorderSum + rightBorderSum
return max(leftMaxSum,rightMaxSum,BorderSum)

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120