-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathImageSmoother.cpp
More file actions
55 lines (50 loc) · 1.5 KB
/
Copy pathImageSmoother.cpp
File metadata and controls
55 lines (50 loc) · 1.5 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public:
vector<vector<int>> imageSmoother(vector<vector<int>>& M) {
int rows = M.size();
if(!rows){
return M;
}
int cols = M[0].size();
vector<vector<int> > ans(rows, vector<int>(cols));
for(int i = 0; i < rows; i++){
for(int j = 0; j < cols; j++){
int curr = M[i][j], count = 1;
if(i-1 >= 0){
curr += M[i-1][j];
count++;
if(j-1 >= 0){
curr += M[i-1][j-1];
count++;
}
if(j+1 < cols){
curr += M[i-1][j+1];
count++;
}
}
if(i+1 < rows){
curr += M[i+1][j];
count++;
if(j-1 >= 0){
curr += M[i+1][j-1];
count++;
}
if(j+1 < cols){
curr += M[i+1][j+1];
count++;
}
}
if(j-1 >= 0){
curr += M[i][j-1];
count++;
}
if(j+1 < cols){
curr += M[i][j+1];
count++;
}
ans[i][j] = curr/count;
}
}
return ans;
}
};