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
.
求最大连续子串和,DP入门题,只有在之前的子情况和大于0的时候,才能加上去!
代码语言:javascript复制class Solution {
public:
int maxSubArray(vector<int>& nums) {
int result=nums[0];
for(int i=1;i<nums.size();i )
{
if(nums[i-1]>0)
nums[i]=nums[i-1] nums[i];
result=max(result,nums[i]);
}
return result;
}
};