Given an integer array nums
, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.
Follow up:
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ if len(nums) == 0: return None dp = nums[0] max_sum = nums[0] for i in nums[1:]: if dp >= 0: dp += i else: dp = i max_sum = max(max_sum,dp) return max_sum
以上