子数组最大和及下标

public void maxSum(int[] nums) {
int start = 0;
int end = 0;
int max = 0;

int temp = 0;
int ts = 0;
for(int i = 0; i < nums.length; i++) {
temp += nums[i];
if(temp < 0) {//出现这种情况时要么出现在i的前面,要么出现在i的后面
ts = i + 1;
temp = 0;
} else {
if(temp > max) {
start = ts;
end = i;
max = temp;
}
}
}

System.out.println("maxSum = " + max + ", start : " + start + ", end = " + end);
}

原文地址:https://www.cnblogs.com/zhaolei1996/p/12523738.html