-
Notifications
You must be signed in to change notification settings - Fork 3
/
661.cpp
30 lines (30 loc) · 827 Bytes
/
661.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
class Solution {
public:
int M;
int N;
int conv(vector<vector<int>>& img, int x, int y) {
int count = 0;
int sum = 0;
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
int _x = x + i;
int _y = y + j;
if (_x < 0 || _x >= M || _y < 0 || _y >= N) continue;
count++;
sum += img[_x][_y];
}
}
return sum / count;
}
vector<vector<int>> imageSmoother(vector<vector<int>>& img) {
M = img.size();
N = img[0].size();
vector<vector<int>> out(M, vector(N, 0));
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
out[i][j] = conv(img, i, j);
}
}
return out;
}
};