给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。

Range Sum Query 2D
上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。

示例:

给定 matrix = [
  [3, 0, 1, 4, 2],
  [5, 6, 3, 2, 1],
  [1, 2, 0, 1, 5],
  [4, 1, 0, 1, 7],
  [1, 0, 3, 0, 5]
]
sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12

说明:

你可以假设矩阵不可变。
会多次调用 sumRegion 方法。
你可以假设 row1 ≤ row2 且 col1 ≤ col2。

方法一:蛮力法。直接将行、列所在的位置进行相加求和。

class NumMatrix {
private:
	vector<vector<int>> matrix;
public:
	NumMatrix(vector<vector<int>> matrix) {
		this->matrix = matrix;//复制矩阵
	}

	int sumRegion(int row1, int col1, int row2, int col2) {
		int result = 0;
        //直接将区域相加
		for (int row = row1; row <= row2; ++row) {
			for (int col = col1; col <= col2; ++col) {
				result += matrix[row][col];
			}
		}
		return result;
	}
};


/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

LeetCode 二维区域和检索
方法二:技巧法。
请先翻阅 LeetCode 区域和检索
不难发现这道题是上一道题的二维化,我们使用类似的优化方法即可。
只优化列,即降为一维问题。
LeetCode 二维区域和检索
LeetCode 二维区域和检索

class NumMatrix {
private:
	vector<vector<int>> matrixSum;//matrixSum[row][col] 表示第row行[0,col]的和
public:
	NumMatrix(vector<vector<int>> matrix) {
		for (auto nums : matrix) {
			//逐行记性求和、复制
            //求[0, i]的各个和
			vector<int> rowSum;
			int tempSum = 0;
			for (auto num : nums) {
				tempSum += num;
				rowSum.push_back(tempSum);
			}
			matrixSum.push_back(rowSum);
		}
	}

	int sumRegion(int row1, int col1, int row2, int col2) {
		int result = 0;
		if (col1 == 0) {//特殊情况
			for (int row = row1; row <= row2; ++row) {
				result += matrixSum[row][col2];
			}
		}
		else {
			for (int row = row1; row <= row2; ++row) {
				result += matrixSum[row][col2] - matrixSum[row][col1 - 1];
			}
		}
		return result;
	}
};


/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

LeetCode 二维区域和检索
优化两个维度。

class NumMatrix {
private:
	vector<vector<int>> matrixSum;//matrixSum[row][col]表示[0, row]行[0,col]的和
public:
	NumMatrix(vector<vector<int>> matrix) {
        int rowSize = matrix.size();
        if (rowSize == 0){
            return;
        }
        int colSize = matrix[0].size();
        if (colSize == 0){
            return;
        }
		for (auto nums : matrix) {
			//逐行记性求和、复制
			vector<int> rowSum;
			int tempSum = 0;
			for (auto num : nums) {
				tempSum += num;
				rowSum.push_back(tempSum);
			}
			matrixSum.push_back(rowSum);
		}
		//再同col下,对[0, row]进行求和
		
		for (int col = 0; col < colSize; ++col) {
			for (int row = 1; row < rowSize; ++row) {
				matrixSum[row][col] += matrixSum[row - 1][col];
			}
		}
	}

	int sumRegion(int row1, int col1, int row2, int col2) {
		if (row1 == 0 && col1 == 0) {
			return matrixSum[row2][col2];
		}
		else if (row1 == 0) {
			return matrixSum[row2][col2] - matrixSum[row2][col1 - 1];
		}
		else if (col1 == 0) {
			return matrixSum[row2][col2] - matrixSum[row1 - 1][col2];
		}
		else {
			return matrixSum[row2][col2] - matrixSum[row2][col1 - 1] - matrixSum[row1 - 1][col2] + matrixSum[row1 - 1][col1 - 1];
		}
	}
};


/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix obj = new NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */

LeetCode 二维区域和检索

相关文章: