-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_0542Matrix01.java
143 lines (126 loc) · 4.35 KB
/
_0542Matrix01.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.heatwave.leetcode.problems;
import java.util.LinkedList;
import java.util.Queue;
/**
* Given an m x n binary matrix mat, return the distance of the nearest 0 for each cell.
* <p>
* The distance between two adjacent cells is 1.
* <p>
*
* <p>
* Example 1:
* <p>
* <p>
* Input: mat = [[0,0,0],[0,1,0],[0,0,0]]
* Output: [[0,0,0],[0,1,0],[0,0,0]]
* Example 2:
* <p>
* <p>
* Input: mat = [[0,0,0],[0,1,0],[1,1,1]]
* Output: [[0,0,0],[0,1,0],[1,2,1]]
*
* <p>
* Constraints:
* <p>
* m == mat.length
* n == mat[i].length
* 1 <= m, n <= 104
* 1 <= m * n <= 104
* mat[i][j] is either 0 or 1.
* There is at least one 0 in mat.
* <p>
* 来源:力扣(LeetCode)
* 链接:https://leetcode.cn/problems/01-matrix
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
public class _0542Matrix01 {
static class Solution {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[][] newMat = new int[m][n];
Queue<Pair<Integer, Integer>> queue = new LinkedList<>();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 0) {
queue.add(new Pair<>(i - 1, j));
queue.add(new Pair<>(i + 1, j));
queue.add(new Pair<>(i, j - 1));
queue.add(new Pair<>(i, j + 1));
}
}
}
int currentDistance = 1;
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- > 0) {
Pair<Integer, Integer> pair = queue.remove();
if (pair.first < 0 || pair.second < 0 || pair.first >= m || pair.second >= n) {
continue;
}
if (mat[pair.first][pair.second] == 0) {
newMat[pair.first][pair.second] = 0;
continue;
}
if (newMat[pair.first][pair.second] != 0) {
continue;
}
newMat[pair.first][pair.second] = currentDistance;
queue.add(new Pair<>(pair.first - 1, pair.second));
queue.add(new Pair<>(pair.first + 1, pair.second));
queue.add(new Pair<>(pair.first, pair.second - 1));
queue.add(new Pair<>(pair.first, pair.second + 1));
}
currentDistance++;
}
return newMat;
}
static class Pair<T, U> {
public Pair(T first, U second) {
this.second = second;
this.first = first;
}
public final T first;
public final U second;
}
}
static class SolutionDP {
public int[][] updateMatrix(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[][] newMat = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
newMat[i][j] = 999999;
if (mat[i][j] == 0) {
newMat[i][j] = 0;
}
}
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i - 1 >= 0) {
newMat[i][j] = Math.min(newMat[i][j], newMat[i - 1][j] + 1);
}
if (j - 1 >= 0) {
newMat[i][j] = Math.min(newMat[i][j], newMat[i][j - 1] + 1);
}
}
}
for (int i = m - 1; i >= 0; i--) {
for (int j = n - 1; j >= 0; j--) {
if (i + 1 < m) {
newMat[i][j] = Math.min(newMat[i][j], newMat[i + 1][j] + 1);
}
if (j + 1 < n) {
newMat[i][j] = Math.min(newMat[i][j], newMat[i][j + 1] + 1);
}
}
}
return newMat;
}
}
public static void main(String[] args) {
Solution solution = new Solution();
int[][] mat = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
solution.updateMatrix(mat);
}
}