子数组最大和及下标

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);
}

posted on 2020-03-19 13:21  努力学习的菜鸡  阅读(188)  评论(0编辑  收藏  举报

导航