Java faster than 95% solution with a rowsum


  • 0
    A

    '''
    public class NumMatrix {

    private int[][] T;
    
    public NumMatrix(int[][] matrix) {
        int rows = matrix.length;
        if(rows == 0){return;}
        int cols = matrix[0].length;
        if(cols == 0){return;}
        
        T = new int[rows+1][cols+1];
        
        for(int r = 0; r < rows; r++){
            int rowsum = 0;
            for(int c = 0; c < cols; c++){
                rowsum += matrix[r][c];
                T[r+1][c+1] = T[r][c+1] + rowsum;
            }
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        return T[row2+1][col2+1] + T[row1][col1] - T[row1][col2+1] - T[row2+1][col1];
    }
    

    }
    '''


Log in to reply
 

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