题目来源:
https://leetcode.com/problems/maximum-subarray/
题意分析:
最大字段和。给出一个数列。输出这个数列的最大字段和。比如:[−2,1,−3,4,−1,2,1,−5,4],最大子段和是[4,-1,2,1]答案是6.
题目思路:
这题用动态规划的思想。b[j] = max{b[j - 1] + a[j],a[j]}
代码(python):
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ size = len(nums) if size == 0: return sum,d = nums[0],0 for i in range(size): if d > 0: d += nums[i] else: d = nums[i] if d > sum: sum = d return sum
转载请注明出处:http://www.cnblogs.com/chruny/p/4968704.html