一、题目描述
输入一个整型数组,数组里有正数也有负数。数组中的一个或连续多个整数组成一个子数组。求所有子数组的和的最大值。
要求时间复杂度为O(n)。
示例1:
输入: nums = [-2,1,-3,4,-1,2,1,-5,4]
输出: 6
解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
提示:
1 <= arr.length <= 10^5
-100 <= arr[i] <= 100
二、代码实现
class Solution {
public int maxSubArray(int[] nums) {
int sum=0,ans=Integer.MIN_VALUE;
for(int num:nums){
sum=Math.max(sum+num,num);
ans=Math.max(ans,sum);
}
return ans;
}
}