Skip to content

Commit d698d9b

Browse files
authored
Merge pull request neetcode-gh#169 from r1cky0/patch-15
Create 73-Set-Matrix-Zeroes.java
2 parents 2a6e405 + 07e4a41 commit d698d9b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

java/73-Set-Matrix-Zeroes.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public void setZeroes(int[][] matrix) {
3+
int rows = matrix.length;
4+
int cols = matrix[0].length;
5+
boolean firstRow = false;
6+
7+
for (int i = 0; i < rows; i++) {
8+
for (int j = 0; j < cols; j++) {
9+
if (matrix[i][j] == 0) {
10+
matrix[0][j] = 0;
11+
if (i == 0) {
12+
firstRow = true;
13+
} else {
14+
matrix[i][0] = 0;
15+
}
16+
}
17+
}
18+
}
19+
20+
for (int i = 1; i < rows; i++) {
21+
for (int j = 1; j < cols; j++) {
22+
if (matrix[0][j] == 0 || matrix[i][0] == 0) {
23+
matrix[i][j] = 0;
24+
}
25+
}
26+
}
27+
28+
if (matrix[0][0] == 0) {
29+
for (int i = 0; i < rows; i++) {
30+
matrix[i][0] = 0;
31+
}
32+
}
33+
34+
if (firstRow) {
35+
for (int j = 0; j < cols; j++) {
36+
matrix[0][j] = 0;
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)