File tree Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Expand file tree Collapse file tree 1 file changed +36
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments