Java 290 ms Solution


  • 0
    F

    """
    class NumMatrix {

    int[][] sum;
    final int[][] orig;
    public NumMatrix(int[][] matrix) {
        orig = matrix;
        if (matrix.length != 0) {
            sum = new int[matrix.length][matrix[0].length];
            int currSum;
            for (int i = 0; i < matrix.length; i++) {
                currSum = 0;
                for (int j = 0; j < matrix[0].length; j++) {
                    currSum += matrix[i][j];
                    sum[i][j] = currSum;
                }
            }
        }
    } 
    
    public void update(int row, int col, int val) {
        int delta = val - orig[row][col];
        orig[row][col] = val;
        for (int j = col; j < sum[0].length; j++) {
            sum[row][j] += delta;
        }
    }
    
    public int sumRegion(int row1, int col1, int row2, int col2) {
        int currSum = 0;
        for (int i = row1; i <= row2; i++) {
            int entireRow = sum[i][col2];
            if (col1 != 0) {
                entireRow -= sum[i][col1 - 1];
            }
            currSum += entireRow;
        }
        return currSum;
    }
    

    }

    """


Log in to reply
 

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