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

代码

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int ans = 0, n = nums.size();
        vector<int> 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;
    }
};
posted @ 2020-07-06 21:39  景云ⁿ  阅读(56)  评论(0编辑  收藏  举报