-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageSmoother.cpp
More file actions
29 lines (28 loc) · 897 Bytes
/
imageSmoother.cpp
File metadata and controls
29 lines (28 loc) · 897 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
// Source: https://leetcode.com/problems/image-smoother/
// Author: Miao Zhang
// Date: 2021-02-26
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int m = M.size();
int n = M[0].size();
vector<vector<int>> res(m, vector<int>(n));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
int cnt = 0;
for (int dx = -1; dx <= 1; dx++) {
for (int dy = -1; dy <= 1; dy++) {
int x = i + dx;
int y = j + dy;
if (x >= 0 && x < m && y >= 0 && y < n) {
res[i][j] += M[x][y];
cnt++;
}
}
}
res[i][j] /= cnt;
}
}
return res;
}
};