-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path73.Set_Matrix_Zeroes.java
36 lines (36 loc) · 1019 Bytes
/
73.Set_Matrix_Zeroes.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
class Solution {
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean firstRow = false;
for (int r=0; r<rows; r++) {
for (int c=0; c<cols; c++) {
if (matrix[r][c] == 0) {
if (r > 0) {
matrix[0][c] = 0;
matrix[r][0] = 0;
} else {
firstRow = true;
}
}
}
}
for (int r=1; r<rows; r++) {
for (int c=1; c<cols; c++) {
if (matrix[0][c] == 0 || matrix[r][0] == 0) {
matrix[r][c] = 0;
}
}
}
if (matrix[0][0] == 0) {
for (int r=0; r<rows; r++) {
matrix[r][0] = 0;
}
}
if (firstRow) {
for (int c=0; c<cols; c++) {
matrix[0][c] = 0;
}
}
}
}