53. Maximum Subarray (dp)

https://leetcode.com/problems/maximum-subarray/description/

题目:求最大字段和

思路: 直接用动态规划即可。

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int max,b=0,len=nums.size();
        for(int x=0;xif(x==0) max=nums[x];
            b=b>0?b+nums[x]:nums[x];
            max=max>b?max:b;
        }
        return max;
    }
};

你可能感兴趣的:(53. Maximum Subarray (dp))