542. 01 Matrix
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]Last updated
Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
Output: [[0,0,0],[0,1,0],[0,0,0]]Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
Output: [[0,0,0],[0,1,0],[1,2,1]]Last updated
public int[][] updateMatrix(int[][] mat) {
int rows = mat.length, cols = mat[0].length;
int max = rows + cols - 2; // 如果只有一个1在角落,这是最大可能的长度
int first, second; // 可能的选择,从上下或者左右过来。也可以另外建一个数组储存结果
// 第一遍检查从上一层或者左边一列过来
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (mat[i][j] == 0) continue; // 如果有另外的数组可以储存,dist[i][j] = 0;
first = max;
second = max;
if (i > 0) first = mat[i - 1][j]; // 从上一层过来。如果有另外的数组可以储存, dist[i][j] = min(dist[i][j], dist[i - 1][j] + 1);
if (j > 0) second = mat[i][j - 1]; // 从左边一列过来。如果有另外的数组可以储存,dist[i][j] = min(dist[i][j], dist[i][j - 1] + 1);
mat[i][j] = Math.min(first, second) + 1;
}
}
// 第二遍检查从下层或者右边一列过来
for (int i = rows - 1; i >= 0; i--) {
for (int j = cols - 1; j >= 0; j--) {
if (mat[i][j] == 0) continue;
first = max;
second = max;
if (i < rows - 1) first = mat[i + 1][j]; // 从下一层过来,也可以 dist[i][j] = min(dist[i][j], dist[i + 1][j] + 1);
if (j < cols - 1) second = mat[i][j + 1]; // 从右边一列过来,也可以 dist[i][j] = min(dist[i][j], dist[i][j + 1] + 1);
mat[i][j] = Math.min(Math.min(first, second) + 1, mat[i][j]); // 如果有另外的数组,不需要这一步
}
}
return mat;
}int[] DIR = new int[]{0, 1, 0, -1, 0};
public int[][] updateMatrix(int[][] mat) {
int rows = mat.length, cols = mat[0].length;
Queue<int[]> q = new LinkedList<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (mat[i][j] == 0) q.offer(new int[]{i,j});
else mat[i][j] = -1; // marked as not processed
}
}
while (!q.isEmpty()) {
int[] curr = q.poll();
int r = curr[0], c = curr[1];
for (int i = 0; i < 4; i++) {
//(0, 1), (1, 0), (0, -1), (-1, 0)
int nextR = r + DIR[i], nextC = c + DIR[i + 1];
if (nextR < 0 || nextR == rows
|| nextC < 0 || nextC == cols
|| mat[nextR][nextC] == -1) continue;
mat[nextR][nextC]= mat[r][c] + 1;
q.offer(new int[]{nextR, nextC});
}
}
return mat;
}