Skip to content

Commit 64632a2

Browse files
29.Matrix Block Sum
1 parent 55edc80 commit 64632a2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

29.Matrix Block Sum.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
// Problem Description: https://leetcode.com/problems/matrix-block-sum/
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> matrixBlockSum(vector<vector<int>>& mat, int k) {
7+
int m = mat.size(), n = mat[0].size();
8+
vector<vector<int>> ans(m, vector<int>(n, 0));
9+
for (int i = 0; i < m; i++)
10+
{
11+
for (int j = 0; j < n; j++)
12+
{
13+
for (int r = max(0, i - k); r <= min(i + k, m - 1); r++)
14+
{
15+
for (int c = max(0, j - k); c <= min(j + k, n - 1); c++)
16+
{
17+
ans[r][c] += mat[i][j];
18+
}
19+
}
20+
}
21+
}
22+
return ans;
23+
}
24+
};

0 commit comments

Comments
 (0)