解题思路
套模板
相关代码
class NumMatrix {
int a[][] = new int[210][210];
int s[][] = new int[210][210];
int m,n;
public NumMatrix(int[][] matrix) {
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
a[i+1][j+1] = matrix[i][j];
}
}
//m表示行,n表示列
m = matrix.length;
n = matrix[0].length;
for(int i=1;i<=matrix.length;i++)
for(int j=1;j<=matrix[0].length;j++)
s[i][j] = s[i][j-1]+s[i-1][j]-s[i-1][j-1]+a[i][j];
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return s[row2+1][col2+1]-s[row2+1][col1]-s[row1][col2+1]+s[row1][col1];
}
}
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/