-
Notifications
You must be signed in to change notification settings - Fork 0
/
304.二维区域和检索-矩阵不可变.cpp
33 lines (29 loc) · 1.02 KB
/
304.二维区域和检索-矩阵不可变.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
* @lc app=leetcode.cn id=304 lang=cpp
*
* [304] 二维区域和检索 - 矩阵不可变
*/
// @lc code=start
class NumMatrix {
private: vector<vector<int>> preSum;
public:
NumMatrix(vector<vector<int>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
if (!m || !n) return; // 需要注意我们要排除特殊情况。
// 接下来就可以构造前缀和矩阵
preSum = vector<vector<int>> (m+1, vector<int> (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];
}
};
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix* obj = new NumMatrix(matrix);
* int param_1 = obj->sumRegion(row1,col1,row2,col2);
*/
// @lc code=end