55. 连续子数组的最大和
原题链接
输入一个 非空 整型数组,数组里的数可能为正,也可能为负。
数组中一个或连续的多个整数组成一个子数组。
求所有子数组的和的最大值。
要求时间复杂度为 O(n)。
数据范围
数组长度 [1,1000]。
代码案例:输入:[1, -2, 3, 10, -4, 7, 2, -5]
输出:18
题解
class Solution {
public int maxSubArray(int[] nums) {
int res =Integer.MIN_VALUE, s = 0;
for (int x : nums)
{
if (s < 0) s = 0;
s += x;
res = Math.max(res, s);
}
return res;
}
}