-
Notifications
You must be signed in to change notification settings - Fork 9
/
SetMatrixZeros.java
76 lines (63 loc) · 1.99 KB
/
SetMatrixZeros.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
import java.util.*;
public class SetMatrixZeros {
public void setZeroes(int[][] matrix) {
if (matrix.length == 0) return;
boolean firstRowZero = false;
boolean firstColZero = false;
for (int i = 0; i < matrix[0].length; i++) {
if (matrix[0][i] == 0) {
firstRowZero = true;
break;
}
}
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][0] == 0) {
firstColZero = true;
break;
}
}
for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
if (matrix[0][j] == 0 || matrix[i][0] == 0)
matrix[i][j] = 0;
}
}
if (firstRowZero) {
for (int i = 0; i < matrix[0].length; i++)
matrix[0][i] = 0;
}
if (firstColZero) {
for (int i = 0; i < matrix.length; i++)
matrix[i][0] = 0;
}
}
public void setZeroes1(int[][] matrix) {
if (matrix.length == 0) return;
int rows = matrix.length;
int cols = matrix[0].length;
Set<Integer> zeroRows = new HashSet<>();
Set<Integer> zeroCols = new HashSet<>();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
zeroRows.add(i);
zeroCols.add(j);
}
}
}
for (int row : zeroRows) {
for (int i = 0; i < cols; i++) matrix[row][i] = 0;
}
for (int col : zeroCols) {
for (int i = 0; i < rows; i++) matrix[i][col] = 0;
}
}
}