53. 最大子序和

 

class Solution {
public:
    int maxSubArray(vector<int>& nums) 
    {
        int sum = 0, ma = nums[0];
        for(int i = 0; i < nums.size(); ++i)
        {
            if(sum < 0)
                sum = nums[i];
            else
                sum += nums[i];
            ma = max(sum, ma);
        }
        return ma;
    }
};

 

上一篇:矩阵


下一篇:2801 Problem A Lake Counting