首页 > 其他分享 >力扣-304-二维区域和检索-矩阵不可变

力扣-304-二维区域和检索-矩阵不可变

时间:2022-12-24 14:45:03浏览次数:46  
标签:检索 matrix int prefixSum 304 力扣 vector size

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

相关文章