File tree Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Expand file tree Collapse file tree 1 file changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public void setZeroes (int [][] matrix ) {
3+ int row =matrix .length ;
4+ int col =matrix [0 ].length ;
5+ int count =0 ;
6+
7+ for (int i =0 ;i <row ;i ++)
8+ {
9+ for (int j =0 ;j <col ;j ++)
10+ {
11+ if (matrix [i ][j ]!=0 )
12+ {
13+ count ++;
14+ }
15+ }
16+ }
17+
18+ if (count ==row *col )
19+ {
20+ return ;
21+ }
22+
23+ for (int i =0 ;i <row ;i ++)
24+ {
25+ for (int j =0 ;j <col ;j ++)
26+ {
27+ if (matrix [i ][j ]==0 )
28+ {
29+ for (int f =i -1 ;f >=0 ;f --)
30+ {
31+ if (matrix [f ][j ]!=0 )
32+ matrix [f ][j ]=-1 ;
33+ }
34+ for (int f =i +1 ;f <row ;f ++)
35+ {
36+ if (matrix [f ][j ]!=0 )
37+ matrix [f ][j ]=-1 ;
38+ }
39+ for (int f =j -1 ;f >=0 ;f --)
40+ {if (matrix [i ][f ]!=0 )
41+ matrix [i ][f ]=-1 ;
42+ }
43+ for (int f =j +1 ;f <col ;f ++)
44+ { if (matrix [i ][f ]!=0 )
45+ matrix [i ][f ]=-1 ;
46+ }
47+ }
48+ }
49+ }
50+
51+
52+ for (int i =0 ;i <row ;i ++)
53+ {
54+ for (int j =0 ;j <col ;j ++)
55+ {
56+ if (matrix [i ][j ]==-1 )
57+ {
58+ matrix [i ][j ]=0 ;
59+ }
60+ }
61+ }
62+
63+ return ;
64+ }
65+ }
You can’t perform that action at this time.
0 commit comments