AC concise java DP solution


  • 0
    B
    private int[][] grid;
    public NumMatrix(int[][] matrix) {
       if(matrix== null || matrix.length == 0 || matrix[0].length == 0)
    	   return;
       grid = new int[matrix.length+1][matrix[0].length+1];
       for(int i = 1;i<=matrix.length;i++){
    	   for(int j=1;j<=matrix[0].length;j++)
    		   grid[i][j] = grid[i-1][j]+grid[i][j-1]-grid[i-1][j-1]+matrix[i-1][j-1];
       }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        return grid[row2+1][col2+1]-grid[row2+1][col1]-grid[row1][col2+1]+grid[row1][col1];
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.