给你一个整数数组 nums ,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
子数组 是数组中的一个连续部分。
class Solution {
public int maxSubArray(int[] nums) {
int max = nums[0];
int pre =0;
for(int i = 0 ; i < nums.length;i++){
pre = Math.max(nums[i],pre+nums[i]);
max = Math.max(max,pre);
}
return max;
}
}