注意:子数组
是数组中的一个连续部分
示例1:
示例2
public class MaximumSbuarray {
public static void main(String[] args) {
int[] nums = new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4};
int pre = nums[0];
int max = nums[0];
int start = 0;
int end = 0;
int startMax = 0;
int endMax = 0;
for (int i = 1; i < nums.length; i++) {
int curNum = nums[i];
//往前走,不断确定序列区间
if (pre + curNum > curNum) {
end = i;
pre += curNum;
} else {
start = i;
end = i;
pre = curNum;
}
//确定最大值
if (pre > max) {
startMax = start;
endMax = end;
max = pre;
}
}
System.out.println("statrMax=" + startMax + "," + "endMax=" + endMax);
System.out.println("max=" + max);
}
}