class NumMatrix {
private int[][] matrix;
private int rows;
private int cols;
public NumMatrix(int[][] matrix) {
if (matrix == null)
this.matrix = null;
rows = matrix.length;
if (rows > 0) {
cols = matrix[0].length;
this.matrix = new int[rows][cols];
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
this.matrix[i][j] = matrix[i][j];
}
}
public int sumRegion(int row1, int col1, int row2, int col2) {
if (matrix == null || row2 > rows || col2 > cols)
return 0;
int sum = 0;
for (int i = row1; i <= row2; i++)
for (int j = col1; j <= col2; j++)
sum += matrix[i][j];
return sum;
}
}
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/