# 最大子矩阵和

0 -2 -7  0
9  2 -6  2
-4  1 -4  1
-1  8  0 -2

9 2
-4 1
-1 8

0 -2 -7  0
9  2 -6  2

9  2 -6  2
-4  1 -4  1

-4  1 -4  1
-1  8  0 -2

public int maxSubsequence(int[] array) {
if (array.length == 0) {
return 0;
}
int max = Integer.MIN_VALUE;
int[] maxSub = new int[array.length];
maxSub[0] = array[0];

for (int i = 1; i < array.length; i++) {
maxSub[i] = (maxSub[i-1] > 0) ? (maxSub[i-1] + array[i]) : array[i];
if (max < maxSub[i]) {
max = maxSub[i];
}
}
return max;
}

9  2 -6  2
-4  1 -4  1
-1  8  0 -2

int[][] total = matrix;
for (int i = 1; i < matrix[0].length; i++) {
for (int j = 0; j < matrix.length; j++) {
total[i][j] += total[i-1][j];
}
}

public int subMaxMatrix(int[][] matrix) {

int[][] total = matrix;
for (int i = 1; i < matrix[0].length; i++) {
for (int j = 0; j < matrix.length; j++) {
total[i][j] += total[i-1][j];
}
}

int maximum = Integer.MIN_VALUE;
for (int i = 0; i < matrix.length; i++) {
for (int j = i; j < matrix.length; j++) {
//result 保存的是从 i 行 到第 j 行 所对应的矩阵上下值的和
int[] result = new int[matrix[0].length];
for (int f = 0; f < matrix[0].length; f++) {
if (i == 0) {
result[f] = total[j][f];
} else {
result[f] = total[j][f] - total[i - 1][f];
}
}
int maximal = maxSubsequence(result);

if (maximal > maximum) {
maximum = maximal;
}
}
}

return maximum;
}

05-19 208

06-13 117

12-09 266

09-07 60

10-14 2277

04-08 765

12-21 6411

07-07 1万+

02-28 3901

07-06 2829

06-22 5707

08-07 952

02-17 163

05-17 562

07-27 256

03-31 341

02-03 372

04-02 517

10-06 300

11-15 1929

01-27 652

03-25 484

02-09 1217

11-09 453

08-16 198

04-19 3072

03-29 104

03-05 203

05-20 811

04-13 134

08-24 227

02-03 297

03-30 3344

03-05 3万+

03-26 3万+

03-22 4万+

08-20 516

03-25 3万+

04-09 2万+

03-30 4045

05-16 609

07-29 36

03-31 4526

05-14 2万+

05-16 5954

03-26 5464

04-06 7万+

07-27 1342

05-07 3万+

05-08 4万+

04-07 5万+

03-10 2万+

03-30 15万+

02-15 18万+

04-14 58万+

03-04 13万+

03-30 4万+

03-05 5万+

05-21 3995

03-01 11万+

03-10 18万+

03-29 9万+

06-12 7万+

04-17 5万+

03-12 10万+

03-01 13万+

03-25 8万+

04-25 6万+

02-15 1万+

02-14 7万+

01-21 28万+

#### Java校招入职华为，半年后我跑路了

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客