原题链接: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