d返回子数组的最大累加和(递推法)
public class d返回子数组的最大累加和 {
public static void main(String[] args) {
int arr[] = new int[] { 1, -2, 3, 5, -2, 6, -1 };
martix1(arr);// 加起来等于负数抛弃法
}
private static void martix1(int[] arr) {// on级别
// TODO Auto-generated method stub
int strmax = arr[0];
int max = strmax;
int left = 0, right = 0;
for (int j = 1; j < arr.length; j++) {
if (strmax >= 0) {
strmax += arr[j];// 左子表的最大和为正,继续后面累加
} else {
strmax = arr[j];
left = j;
}
if (strmax > max) {
max = strmax;
right = j;
}
}
System.out.println(max);
}
}