-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
43 lines (40 loc) · 1.14 KB
/
solution.js
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
/**
* @param {number[][]} M
* @return {number[][]}
*/
var imageSmoother = function(M) {
let res = []
for (let i = 0; i < M.length; i++) {
let row = []
for (let j = 0; j < M[0].length; j++) {
let points = [M[i][j]]
if (j >= 1) {
points.push(M[i][j - 1])
}
if (j + 1 < M[0].length) {
points.push(M[i][j + 1])
}
if (i >= 1) {
points.push(M[i - 1][j])
if (j >= 1) {
points.push(M[i - 1][j - 1])
}
if (j + 1 < M[0].length) {
points.push(M[i - 1][j + 1])
}
}
if (i + 1 < M.length) {
points.push(M[i + 1][j])
if (j >= 1) {
points.push(M[i + 1][j - 1])
}
if (j + 1 < M[0].length) {
points.push(M[i + 1][j + 1])
}
}
row.push(parseInt(points.reduce((x, y) => x + y) / points.length))
}
res.push(row)
}
return res
};