题目来源:最大子数组
题目描述:
给定一个整数数组,找到一个具有最大和的子数组,返回其最大和。
样例:
给出数组[−2,2,−3,4,−1,2,1,−5,3],符合要求的子数组为[4,−1,2,1],其最大和为6
Java代码:
public int maxSubArray(int[] nums) {
// write your code
int start=0,end=0,mixsum=nums[0],nowsum=0;
while (end<nums.length) {
if(nowsum<=0)
{
nowsum=nums[end];
end++;
start=end;
}else
{
nowsum+=nums[end];
end++;
}
if(nowsum>mixsum)
mixsum=nowsum;
}
return mixsum;
}