File tree Expand file tree Collapse file tree 4 files changed +65
-0
lines changed
CPP/arrays/1. Set Matrix Zero Expand file tree Collapse file tree 4 files changed +65
-0
lines changed Original file line number Diff line number Diff line change 1+ 3 4
2+ 0 1 2 0 3 4 5 2 1 3 1 5
Original file line number Diff line number Diff line change 1+ 0 0 0 0
2+ 0 4 5 0
3+ 0 3 1 0
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments