01 矩阵
难度:
标签:
题目描述
给定一个由 0
和 1
组成的矩阵 mat
,请输出一个大小相同的矩阵,其中每一个格子是 mat
中对应位置元素到最近的 0
的距离。
两个相邻元素间的距离为 1
。
示例 1:
输入:mat = [[0,0,0],[0,1,0],[0,0,0]] 输出:[[0,0,0],[0,1,0],[0,0,0]]
示例 2:
输入:mat = [[0,0,0],[0,1,0],[1,1,1]] 输出:[[0,0,0],[0,1,0],[1,2,1]]
提示:
m == mat.length
n == mat[i].length
1 <= m, n <= 104
1 <= m * n <= 104
mat[i][j] is either 0 or 1.
mat
中至少有一个0
代码结果
运行时间: 99 ms, 内存: 18.2 MB
/*
* 题目思路:
* 使用Java Stream API处理和计算矩阵中各个元素到最近0的距离。
* 1. 使用Queue储存所有为0的点,将其余点设为无穷大(Integer.MAX_VALUE)。
* 2. BFS遍历矩阵,用Stream进行迭代和处理。
*/
import java.util.*;
import java.util.stream.*;
public class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int[][] dist = new int[m][n];
Queue<int[]> queue = new LinkedList<>();
IntStream.range(0, m).forEach(i -> IntStream.range(0, n).forEach(j -> {
if (mat[i][j] == 0) {
queue.offer(new int[]{i, j});
dist[i][j] = 0;
} else {
dist[i][j] = Integer.MAX_VALUE;
}
}));
int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
while (!queue.isEmpty()) {
int[] cell = queue.poll();
Arrays.stream(dirs).forEach(dir -> {
int r = cell[0] + dir[0];
int c = cell[1] + dir[1];
if (r >= 0 && r < m && c >= 0 && c < n && dist[r][c] > dist[cell[0]][cell[1]] + 1) {
queue.offer(new int[]{r, c});
dist[r][c] = dist[cell[0]][cell[1]] + 1;
}
});
}
return dist;
}
}
解释
方法:
该题解采用了广度优先搜索(BFS)的方法。首先,遍历整个矩阵,将所有为0的位置坐标存储到一个列表中,这些位置作为BFS的起始节点。使用一个队列来执行BFS,队列的初始内容是所有0的位置。在BFS过程中,从队列中取出位置,并将其四周的相邻位置(上、下、左、右)加入队列中,同时记录从起始节点到当前节点的距离。通过这种方式,可以确保每个元素到最近的0的距离被正确计算并存储在结果矩阵中。
时间复杂度:
O(m * n)
空间复杂度:
O(m * n)
代码细节讲解
🦆
为什么在算法中首先需要将矩阵中所有的0的位置存储起来作为BFS的起始节点?
▷🦆
在BFS过程中,你是如何确保不会重复访问同一个节点?
▷🦆
你的算法中提到了一个变量`level`,它是如何帮助记录从起始节点到当前节点的距离的?
▷