forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0073-set-matrix-zeroes.java
41 lines (36 loc) · 1.02 KB
/
0073-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
37
38
39
40
41
class Solution {
public void setZeroes(int[][] matrix) {
int rows = matrix.length;
int cols = matrix[0].length;
boolean firstRow = false;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == 0) {
matrix[0][j] = 0;
if (i == 0) {
firstRow = true;
} else {
matrix[i][0] = 0;
}
}
}
}
for (int i = 1; i < rows; i++) {
for (int j = 1; j < cols; j++) {
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
matrix[i][j] = 0;
}
}
}
if (matrix[0][0] == 0) {
for (int i = 0; i < rows; i++) {
matrix[i][0] = 0;
}
}
if (firstRow) {
for (int j = 0; j < cols; j++) {
matrix[0][j] = 0;
}
}
}
}