二维数组的连续子数组的最大和
01 public static int[][] arrSum(int arr[][]){ 02 int m = arr.length; 03 int n = arr[0].length; 04 int p[][] = new int[m+1][n+1]; 05 p[0][0] = arr[0][0]; 06 for(int i=0; i<=m; i++) p[i][0] = 0; 07 for(int i=0; i<=n; i++) p[0][i] = 0; 08 for(int i=1; i<=m; i++){ 09 for(int j=1; j<=n; j++){ 10 p[i][j] = p[i-1][j] + p[i][j-1] + arr[i-1][j-1] - p[i-1][j-1]; 11 } 12 } 13 return p; 14 } 15 //遍历所有二维数组的矩形区域 16 static int maxArrSum(int arr[][]){ 17 int m = arr.length; 18 int n = arr[0].length; 19 int p[][] = arrSum(arr); 20 int ans = Integer.MIN_VALUE; 21 for(int i=1; i<=m; i++){ 22 for(int j=1; j<=n; j++){ 23 for(int endi=i; endi <=m; endi++){ 24 for(int endj=j; endj<=n; endj++){ 25 int sum = p[endi][endj] - p[i-1][endj] - p[endi][j-1] + p[i-1][j-1]; 26 if(ans < sum) ans = sum; 27 } 28 } 29 } 30 } 31 return ans; 32 }