虽然 leecode 说是 esay, 但确实想了好久
class Solution {
public:
// hit:最大值一直保有,有了更大的才交换
int maxSubArray(vector<int>& nums) {
size_t size = nums.size();
int maxSum = INT_MIN;
int sum = 0;
for(size_t i = 0; i != size; ++i) {
sum += nums[i];
maxSum = max(maxSum, sum);
if (sum < 0) {
sum = 0;
}
}
return maxSum;
}
};