-
Notifications
You must be signed in to change notification settings - Fork 0
/
19.js
57 lines (57 loc) · 1.41 KB
/
19.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @param {number[][]} img
* @return {number[][]}
*/
var imageSmoother = function (g) {
const m = g.length
const n = g[0].length
const g2 = Array.from({ length: m }, (e) => new Array(n))
for (let currRow = 0; currRow < m; currRow++) {
const previousRow = currRow - 1
const nextRow = currRow + 1
for (let currCol = 0; currCol < n; currCol++) {
const previousCol = currCol - 1
const nextCol = currCol + 1
let sum = 0
let count = 0
if (previousRow >= 0) {
if (previousCol >= 0) {
sum += g[previousRow][previousCol]
count++
}
sum += g[previousRow][currCol]
count++
if (nextCol < n) {
sum += g[previousRow][nextCol]
count++
}
}
if (currRow >= 0 && currRow < m) {
if (previousCol >= 0) {
sum += g[currRow][previousCol]
count++
}
sum += g[currRow][currCol] || 0
count++
if (nextCol < n) {
sum += g[currRow][nextCol] || 0
count++
}
}
if (nextRow < m) {
if (previousCol >= 0) {
sum += g[nextRow][previousCol] || 0
count++
}
sum += g[nextRow][currCol] || 0
count++
if (nextCol < n) {
sum += g[nextRow][nextCol] || 0
count++
}
}
g2[currRow][currCol] = Math.floor(sum / count)
}
}
return g2
}