-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1314_Matrix_Block_Sum.cpp
More file actions
19 lines (19 loc) · 977 Bytes
/
Copy path1314_Matrix_Block_Sum.cpp
File metadata and controls
19 lines (19 loc) · 977 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int K) {
vector<vector<int>> answer(
(mat.size()),
*(new vector<int>(mat[0].size(), 0))
);
for (uint8_t i = 0; i < answer.size(); i++) {
for (uint8_t j = 0; j < answer[i].size(); j++) {
for (uint8_t r = (i - K < 0? 0 : i - K); r <= (i + K >= mat.size()? mat.size() - 1 : i + K); r++) {
for (uint8_t c = (j - K < 0? 0 : j - K); c <= (j + K >= mat[0].size()? mat[0].size() - 1 : j + K); c++) {
answer[i][j] += mat[r][c];
}
}
}
}
return answer;
}
};