-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode542.java
More file actions
62 lines (58 loc) · 2.13 KB
/
LeetCode542.java
File metadata and controls
62 lines (58 loc) · 2.13 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
import java.util.Arrays;
import java.util.Queue;
import java.util.LinkedList;
public class LeetCode542 {
public static void main(String[] args) {
// 输入:mat = [[0,0,0],[0,1,0],[0,0,0]]
// 输出:[[0,0,0],[0,1,0],[0,0,0]]
System.out.println(
Arrays.deepToString(
new Solution542().updateMatrix(new int[][] { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 0, 0 } })));
// 输入:mat = [[0,0,0],[0,1,0],[1,1,1]]
// 输出:[[0,0,0],[0,1,0],[1,2,1]]
System.out.println(
Arrays.deepToString(
new Solution542().updateMatrix(new int[][] { { 0, 0, 0 }, { 0, 1, 0 }, { 1, 1, 1 } })));
// 输入:mat = [[0],[1]]
// 输出:[[0],[1]]
System.out.println(
Arrays.deepToString(
new Solution542().updateMatrix(new int[][] { { 0 }, { 1 } })));
}
}
class Solution542 {
public int[][] updateMatrix(int[][] mat) {
int[][] dirs = new int[][] { { 0, 1 }, { 0, -1 }, { 1, 0 }, { -1, 0 } };
Queue<int[]> queue = new LinkedList<int[]>();
int m = mat.length;
int n = mat[0].length;
int[][] res = new int[m][n];
boolean[][] visited = new boolean[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0) {
queue.offer(new int[] { i, j });
visited[i][j] = true;
}
}
}
int cost = 0;
while (!queue.isEmpty()) {
int size = queue.size();
for (int s = 0; s < size; s++) {
int[] cur = queue.poll();
res[cur[0]][cur[1]] = cost;
for (int[] dir : dirs) {
int x = cur[0] + dir[0];
int y = cur[1] + dir[1];
if (x >= 0 && x < m && y >= 0 && y < n && !visited[x][y]) {
queue.offer(new int[] { x, y });
visited[x][y] = true;
}
}
}
cost++;
}
return res;
}
}