说明
采用这种方法虽然简短,但是比较耗时,可以借鉴官方的方法。
class NumMatrix(object): def __init__(self, matrix): """ :type matrix: List[List[int]] """ self.matrix = matrix def sumRegion(self, row1, col1, row2, col2): """ :type row1: int :type col1: int :type row2: int :type col2: int :rtype: int """ self.sums = 0 for i in range(row1,row2 + 1): self.sums += sum(self.matrix[i][col1:col2+1]) return self.sums