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