File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ void setZeroes (vector<vector<int >>& matrix) {
4+ int rows = matrix.size ();
5+ int cols = matrix[0 ].size ();
6+
7+ bool firstRowZero = false ;
8+ bool firstColZero = false ;
9+
10+ for (int j = 0 ; j < cols; j++){
11+ if (matrix[0 ][j] == 0 )
12+ firstRowZero = true ;
13+ }
14+
15+ for (int i = 0 ; i < rows; i++){
16+ if (matrix[i][0 ] == 0 )
17+ firstColZero = true ;
18+ }
19+
20+ for (int i = 1 ; i < rows; i++){
21+ for (int j = 1 ; j < cols; j++){
22+ if (matrix[i][j] == 0 ){
23+ matrix[i][0 ] = 0 ;
24+ matrix[0 ][j] = 0 ;
25+ }
26+ }
27+ }
28+
29+ for (int i = 1 ; i < matrix.size (); i++){
30+ for (int j = 1 ; j < matrix[0 ].size (); j++){
31+ if (matrix[0 ][j] == 0 || matrix[i][0 ] == 0 )
32+ matrix[i][j] = 0 ;
33+ }
34+ }
35+
36+ if (firstRowZero){
37+ for (int j = 0 ; j < cols; j++)
38+ matrix[0 ][j] = 0 ;
39+ }
40+
41+ if (firstColZero){
42+ for (int i = 0 ; i < rows; i++)
43+ matrix[i][0 ] = 0 ;
44+ }
45+ }
46+ };
You can’t perform that action at this time.
0 commit comments