首页 > 其他分享 >LC[53] 最大子数组和

LC[53] 最大子数组和

时间:2022-11-21 22:57:53浏览次数:85  
标签:LC nums res long 53 数组 ans

原题链接:https://leetcode.cn/problems/maximum-subarray/description/

WA了一次,是因为没有考虑到负数的情况

AC代码:

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        long long ans = 0, res = -0x3f3f3f3f;
        for (auto x : nums)
        {
            if (ans < 0)
                ans = 0;
            ans += x;
            res = max(res, ans);
        }
        return res;
    }
};

标签:LC,nums,res,long,53,数组,ans
From: https://www.cnblogs.com/StarTwinkle/p/16913553.html

相关文章