-
Notifications
You must be signed in to change notification settings - Fork 20
/
SetMatrixZeroes.java
executable file
·42 lines (35 loc) · 1.05 KB
/
SetMatrixZeroes.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
import java.util.HashSet;
import java.util.Set;
/**
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
* <p>
* Accepted.
*/
public class SetMatrixZeroes {
public void setZeroes(int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return;
}
Set<Integer> row = new HashSet<>(matrix.length);
Set<Integer> column = new HashSet<>(matrix[0].length);
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
row.add(i);
column.add(j);
}
}
}
for (int i : row) {
for (int k = 0; k < matrix[0].length; k++) {
matrix[i][k] = 0;
}
}
for (int i : column) {
for (int k = 0; k < matrix.length; k++) {
matrix[k][i] = 0;
}
}
// System.out.println(Arrays.deepToString(matrix));
}
}