Skip to content

Commit d5bbe9a

Browse files
Merge pull request #70 from RealAdarsh/cppCode
added set matrix zero problem
2 parents 20719cc + 5e36636 commit d5bbe9a

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
3 4
2+
0 1 2 0 3 4 5 2 1 3 1 5
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
0 0 0 0
2+
0 4 5 0
3+
0 3 1 0
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
5+
void setZeroes(vector<vector<int>> &matrix) {
6+
7+
//unordered_set <int> row;
8+
//unordered_set <int> col;
9+
10+
int n=matrix.size();
11+
int m=matrix[0].size();
12+
int col0=1;
13+
14+
for (int i=0; i<n; i++){
15+
if (matrix[i][0]==0) col0=0;
16+
for (int j=1; j<m; j++){
17+
if (matrix[i][j]==0){
18+
matrix[i][0]=0;
19+
matrix[0][j]=0;
20+
}
21+
}
22+
}
23+
24+
for (int i=n-1; i>=0; i--){
25+
26+
for (int j=m-1; j>=1; j--){
27+
if (matrix[i][0]==0 || matrix[0][j]==0){
28+
matrix[i][j]=0;
29+
}
30+
}
31+
if (col0==0) matrix[i][0]=0;
32+
}
33+
34+
}
35+
36+
int main(){
37+
38+
int N;
39+
cin >>N;
40+
int M;
41+
cin >>M;
42+
vector<vector<int>> matrix(N,vector <int> (M));
43+
44+
for (int i=0; i<N; i++){
45+
for (int j=0; j<M; j++){
46+
cin >> matrix[i][j];
47+
}
48+
}
49+
50+
setZeroes(matrix);
51+
52+
for (int i=0; i<N; i++){
53+
for (int j=0; j<M; j++){
54+
cout << matrix[i][j] << " ";
55+
}
56+
cout << endl;
57+
}
58+
59+
return 0;
60+
}
80.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)