LeetCode[53] 最大子数组和
原题链接: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;
}
};