Skip to content

Commit 5478114

Browse files
committed
Set Matrix Zeroes solution
1 parent fc886c4 commit 5478114

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

set-matrix-zeroes/PDKhan.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
};

0 commit comments

Comments
 (0)