-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path304.cpp
More file actions
30 lines (28 loc) · 973 Bytes
/
Copy path304.cpp
File metadata and controls
30 lines (28 loc) · 973 Bytes
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
class NumMatrix {
public:
NumMatrix(vector<vector<int>> matrix) {
int n = matrix.size();
if (n != 0) {
int m = matrix[0].size();
for (int i = 0; i < n; ++i) {
int tempSum = 0;
vector<int> temp;
for (int j = 0; j < m; ++j) {
tempSum += matrix[i][j];
temp.push_back(i == 0 ? tempSum : (tempSum + sum[i - 1][j]));
}
sum.push_back(temp);
}
}
}
int sumRegion(int row1, int col1, int row2, int col2) {
if (row1 == 0 && col1 == 0)
return sum[row2][col2];
else if (row1 == 0)
return sum[row2][col2] - sum[row2][col1 - 1];
else if (col1 == 0)
return sum[row2][col2] - sum[row1 - 1][col2];
return sum[row2][col2] - sum[row1 - 1][col2] - sum[row2][col1 - 1] + sum[row1 - 1][col1 - 1];
}
vector<vector<int>> sum;
};