class NumMatrix {
private:
vector<vector<int>> prefixSum;
public:
NumMatrix(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
// 第一行、第一列均为额外的空行,为了初始化方便
prefixSum.resize(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
prefixSum[i][j] = prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1] + matrix[i - 1][j - 1];
}
int sumRegion(int row1, int col1, int row2, int col2) {
return prefixSum[row2 + 1][col2 + 1] - prefixSum[row1][col2 + 1] - prefixSum[row2 + 1][col1] + prefixSum[row1][col1];
}
};
标签:检索,matrix,int,prefixSum,304,力扣,vector,size
From: https://www.cnblogs.com/yaocy/p/17002850.html