/**
* very similar with range sum in 1D array, need a 2D array to save the intermediate values
* instantiate a row+1, col+1 array make lot easier*
*/
public class NumMatrix {
private int[][] dp;
public NumMatrix(int[][] matrix) {
if( matrix == null
|| matrix.length == 0
|| matrix[0].length == 0 ){
return;
}
int row = matrix.length;
int col = matrix[0].length;
dp = new int[row+1][col+1];
for (int i=1; i<=row; i++)
for (int j=1; j<=col; j++)
dp[i][j] = dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+matrix[i-1][j-1];
}
public int sumRegion(int row1, int col1, int row2, int col2) {
return dp[row2+1][col2+1] - dp[row1][col2+1] - dp[row2+1][col1] + dp[row1][col1];
}
}
// Your NumMatrix object will be instantiated and called as such:
// NumMatrix numMatrix = new NumMatrix(matrix);
// numMatrix.sumRegion(0, 1, 2, 3);
// numMatrix.sumRegion(1, 2, 3, 4);
Leetcode 304. Range Sum Query 2D - Immutable
最新推荐文章于 2024-01-13 16:19:34 发布