Skip to content

Commit e5ce99e

Browse files
authored
Create SetMatrixZeroes.java
1 parent b7c9f09 commit e5ce99e

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

SetMatrixZeroes.java

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

0 commit comments

Comments
 (0)