forked from varunu28/LeetCode-Java-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage Smoother.java
More file actions
49 lines (45 loc) · 1.42 KB
/
Image Smoother.java
File metadata and controls
49 lines (45 loc) · 1.42 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
class Solution {
public int[][] imageSmoother(int[][] M) {
int[][] res = new int[M.length][M[0].length];
for (int i=0;i<M.length;i++) {
for (int j=0;j<M[0].length;j++) {
int count = 1;
int sum = M[i][j];
if(j-1 >= 0) {
count++;
sum += M[i][j-1];
}
if(j+1 < M[0].length) {
count++;
sum += M[i][j+1];
}
if(i-1 >= 0) {
count++;
sum += M[i-1][j];
}
if(i+1 < M.length) {
count++;
sum += M[i+1][j];
}
if(i+1 < M.length && j+1 < M[0].length) {
count++;
sum += M[i+1][j+1];
}
if(i+1 < M.length && j-1 >= 0) {
count++;
sum += M[i+1][j-1];
}
if(i-1 >= 0 && j-1 >= 0) {
count++;
sum += M[i-1][j-1];
}
if(i-1 >= 0 && j+1 < M[0].length) {
count++;
sum += M[i-1][j+1];
}
res[i][j] = (int)Math.floor(sum/count);
}
}
return res;
}
}