leetcode-----53. 最大子序和

  • 链接:https://leetcode-cn.com/problems/maximum-subarray/

代码

class Solution {
public:
    int maxSubArray(vector& nums) {
        int ans = 0, n = nums.size();
        vector f(n);
        f[0] = nums[0];
        ans = f[0];

        for (int i = 1; i < n; ++i) {
            f[i] = max(f[i - 1] + nums[i], nums[i]);
            ans = max(ans, f[i]);
        }
        return ans;
    }
};

你可能感兴趣的:(leetcode-----53. 最大子序和)