2 Star 10 Fork 2

CG国斌 / myleetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
_542.java 1.78 KB
一键复制 编辑 原始数据 按行查看 历史
Charies Gavin 提交于 2020-02-06 12:44 . 初始化 myleetcode 项目
package com.hit.basmath.learn.queue_stack;
import java.util.LinkedList;
import java.util.Queue;
/**
* 542. 01 Matrix
* <p>
* Given a matrix consists of 0 and 1, find the distance of the nearest 0 for each cell.
* <p>
* The distance between two adjacent cells is 1.
* <p>
* Example 1:
* <p>
* Input:
* <p>
* [[0,0,0],
* [0,1,0],
* [0,0,0]]
* <p>
* Output:
* <p>
* [[0,0,0],
* [0,1,0],
* [0,0,0]]
* <p>
* Example 2:
* <p>
* Input:
* <p>
* [[0,0,0],
* [0,1,0],
* [1,1,1]]
* <p>
* Output:
* <p>
* [[0,0,0],
* [0,1,0],
* [1,2,1]]
* <p>
* Note:
* <p>
* 1. The number of elements of the given matrix will not exceed 10,000.
* 2. There are at least one 0 in the given matrix.
* 3. The cells are adjacent in only four directions: up, down, left and right.
*/
public class _542 {
public int[][] updateMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
Queue<int[]> queue = new LinkedList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
queue.offer(new int[]{i, j});
} else {
matrix[i][j] = Integer.MAX_VALUE;
}
}
}
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
while (!queue.isEmpty()) {
int[] cell = queue.poll();
for (int[] d : dirs) {
int r = cell[0] + d[0];
int c = cell[1] + d[1];
if (r < 0 || r >= m || c < 0 || c >= n ||
matrix[r][c] <= matrix[cell[0]][cell[1]] + 1) continue;
queue.add(new int[]{r, c});
matrix[r][c] = matrix[cell[0]][cell[1]] + 1;
}
}
return matrix;
}
}
Java
1
https://gitee.com/guobinhit/myleetcode.git
git@gitee.com:guobinhit/myleetcode.git
guobinhit
myleetcode
myleetcode
master

搜索帮助