0542. 01 矩阵【中等】
1. 📝 题目描述
给定一个由 0 和 1 组成的矩阵 mat,请输出一个大小相同的矩阵,其中每一个格子是 mat 中对应位置元素到最近的 0 的距离。
两个相邻元素间的距离为 1。
示例 1:

txt
输入:mat = [[0,0,0],[0,1,0],[0,0,0]]
输出:[[0,0,0],[0,1,0],[0,0,0]]1
2
2
示例 2:

txt
输入:mat = [[0,0,0],[0,1,0],[1,1,1]]
输出:[[0,0,0],[0,1,0],[1,2,1]]1
2
2
提示:
m == mat.lengthn == mat[i].length1 <= m, n <= 10^41 <= m * n <= 10^4mat[i][j] is either 0 or 1.mat中至少有一个0
2. 🎯 s.1 - 多源 BFS
c
int** updateMatrix(int** mat, int matSize, int* matColSize, int* returnSize, int** returnColumnSizes) {
int m = matSize, n = matColSize[0];
int** dist = (int**)malloc(sizeof(int*) * m);
int* queue = (int*)malloc(sizeof(int) * m * n * 2);
int head = 0, tail = 0;
for (int i = 0; i < m; i++) {
dist[i] = (int*)malloc(sizeof(int) * n);
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0) {
dist[i][j] = 0;
queue[tail++] = i;
queue[tail++] = j;
} else {
dist[i][j] = m + n;
}
}
}
int dirs[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};
while (head < tail) {
int x = queue[head++], y = queue[head++];
for (int d = 0; d < 4; d++) {
int nx = x + dirs[d][0], ny = y + dirs[d][1];
if (nx >= 0 && nx < m && ny >= 0 && ny < n && dist[nx][ny] > dist[x][y] + 1) {
dist[nx][ny] = dist[x][y] + 1;
queue[tail++] = nx;
queue[tail++] = ny;
}
}
}
free(queue);
*returnSize = m;
*returnColumnSizes = (int*)malloc(sizeof(int) * m);
for (int i = 0; i < m; i++) (*returnColumnSizes)[i] = n;
return dist;
}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
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
js
/**
* @param {number[][]} mat
* @return {number[][]}
*/
var updateMatrix = function (mat) {
const m = mat.length,
n = mat[0].length
const dist = Array.from({ length: m }, () => new Array(n).fill(Infinity))
const queue = []
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j] === 0) {
dist[i][j] = 0
queue.push([i, j])
}
}
}
const dirs = [
[1, 0],
[-1, 0],
[0, 1],
[0, -1],
]
let head = 0
while (head < queue.length) {
const [x, y] = queue[head++]
for (const [dx, dy] of dirs) {
const nx = x + dx,
ny = y + dy
if (
nx >= 0 &&
nx < m &&
ny >= 0 &&
ny < n &&
dist[nx][ny] > dist[x][y] + 1
) {
dist[nx][ny] = dist[x][y] + 1
queue.push([nx, ny])
}
}
}
return dist
}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
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
py
class Solution:
def updateMatrix(self, mat: List[List[int]]) -> List[List[int]]:
m, n = len(mat), len(mat[0])
dist = [[float('inf')] * n for _ in range(m)]
queue = deque()
for i in range(m):
for j in range(n):
if mat[i][j] == 0:
dist[i][j] = 0
queue.append((i, j))
while queue:
x, y = queue.popleft()
for dx, dy in ((1,0),(-1,0),(0,1),(0,-1)):
nx, ny = x + dx, y + dy
if 0 <= nx < m and 0 <= ny < n and dist[nx][ny] > dist[x][y] + 1:
dist[nx][ny] = dist[x][y] + 1
queue.append((nx, ny))
return dist1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- 时间复杂度:
,其中 m 和 n 是矩阵的行数和列数 - 空间复杂度:
算法思路:
- 将所有 0 元素作为起点加入队列,距离初始化为 0
- BFS 逐层向外扩展,更新每个 1 元素到最近 0 的距离