Simple Python Solution


  • 0
    class NumMatrix(object):
        def __init__(self, matrix):
            m = len(matrix)
            n = len(matrix[0]) if m else 0
            self.sumt = [[0] * (n+1) for _ in range(m+1)]
            for i in range(m):
                for j in range(n):
                    self.sumt[i+1][j+1] = self.sumt[i+1][j] + self.sumt[i][j+1] - self.sumt[i][j] + matrix[i][j]
    
        def sumRegion(self, r1, c1, r2, c2):
            return self.sumt[r2+1][c2+1] - self.sumt[r2+1][c1] - self.sumt[r1][c2+1] + self.sumt[r1][c1]

Log in to reply
 

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