-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix01.java
54 lines (43 loc) · 1.32 KB
/
matrix01.java
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
package week3;
/***
* Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
*
* The distance between two adjacent cells is 1.
* 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]]
*/
public class matrix01 {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length;
int n = mat[0].length;
int maxLen = m+n;
int[][] result = new int[m][n];
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(mat[i][j]==0) continue;
result[i][j] = maxLen;
if(i>0){
result[i][j] = Math.min(result[i][j], result[i-1][j]+1);
}
if(j>0){
result[i][j] = Math.min(result[i][j], result[i][j-1]+1);
}
}
}
for(int i=m-1;i>=0;i--){
for(int j=n-1;j>=0;j--){
if(mat[i][j]==0) continue;
if(i<m-1){
result[i][j] = Math.min(result[i][j], result[i+1][j]+1);
}
if(j<n-1){
result[i][j] = Math.min(result[i][j], result[i][j+1]+1);
}
}
}
return result;
}
}