-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathleetcode-542-01Matrix-2.js
More file actions
49 lines (44 loc) · 896 Bytes
/
leetcode-542-01Matrix-2.js
File metadata and controls
49 lines (44 loc) · 896 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @param {number[][]} mat
* @return {number[][]}
*/
// p: grid m x n;
// r: grid m x n;
// e:
//
//
// [[0,0,0],
// [0,1,0],
// [1,1,1]]
//
//
var updateMatrix = function (mat) {
const queue = [];
for (let i = 0; i < mat.length; i++) {
for (let j = 0; j < mat[0].length; j++) {
if (mat[i][j] === 0) {
queue.push([i, j, 0]);
} else {
mat[i][j] = -1;
}
}
}
while (queue.length > 0) {
const [y, x, d] = queue.shift();
const deltas = [
[y + 1, x],
[y - 1, x],
[y, x + 1],
[y, x - 1],
];
for (let [neiY, neiX] of deltas) {
const boundY = 0 <= neiY && neiY < mat.length;
const boundX = 0 <= neiX && neiX < mat[0].length;
if (boundY && boundX && mat[neiY][neiX] === -1) {
mat[neiY][neiX] = d + 1;
queue.push([neiY, neiX, d + 1]);
}
}
}
return mat;
};