-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlc-542.java
More file actions
29 lines (26 loc) · 1.02 KB
/
lc-542.java
File metadata and controls
29 lines (26 loc) · 1.02 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
class Solution {
public int[][] updateMatrix(int[][] m) {
int[][] res = new int[m.length][m[0].length];
Queue<int[]> q = new LinkedList();
for(int i = 0; i<res.length; i++) {
for(int j = 0; j<res[0].length; j++) {
res[i][j] = m[i][j] == 0?0:Integer.MAX_VALUE; //初始化结果矩阵,0元素为0,非零元素为无穷
if(res[i][j] == 0) q.offer(new int[]{i, j});
}
}
int[][] move = new int[][]{{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
while(!q.isEmpty()) {
int[] n = q.poll();
for(int[] mv : move) {
int ni = n[0] + mv[0];
int nj = n[1] + mv[1];
if(ni >= 0 && ni < res.length && nj >= 0 && nj < m[0].length) {
if(res[ni][nj] > res[n[0]][n[1]]) res[ni][nj] = res[n[0]][n[1]] + 1;
else continue;
q.offer(new int[]{ni, nj});
}
}
}
return res;
}
}