class NumMatrix { private: vector> preSum; public: NumMatrix(vector >& matrix) { int m = matrix.size(), n = matrix[0].size(); if (m == 0 || n == 0) return; preSum.resize(m + 1, vector (n + 1)); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) preSum[i][j] = preSum[i - 1][j] + preSum[i][j - 1] + matrix[i - 1][j - 1] - preSum[i - 1][j - 1]; } int sumRegion(int row1, int col1, int row2, int col2) { return preSum[row2 + 1][col2 + 1] - preSum[row1][col2 + 1] - preSum[row2 + 1][col1] + preSum[row1][col1]; } };
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)