-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix_zeros.cpp
More file actions
71 lines (57 loc) · 1.44 KB
/
Copy pathmatrix_zeros.cpp
File metadata and controls
71 lines (57 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Given an nxm matrix of integers, zero-out the columns and the rows
of any elements equal to zero.
sample input:
3 1 8 9 2
7 3 3 0 1
8 4 5 2 8
6 0 9 5 1
output:
3 0 8 0 2
0 0 0 0 0
8 0 5 0 8
0 0 0 0 0
*/
#include <iostream>
#include <vector>
#include <unordered_set>
using std::cout;
using std::vector;
using std::unordered_set;
int main()
{
vector<vector<int>> M = { {3,1,8,9,2},
{7,3,3,0,1},
{8,4,5,2,8},
{6,0,9,5,1} };
// traverse the matrix, saving i,j indexes of zero elements to unordered_sets
int i,j;
unordered_set<int> usetRows;
unordered_set<int> usetCols;
for (i=0; i<M.size(); i++) {
for (j=0; j<M[i].size(); j++) {
if (M[i][j] == 0) {
usetRows.insert(i);
usetCols.insert(j);
}
}
}
// iterate over the zeros, if any
if (usetRows.size()) {
for (auto& e:usetRows) { // zero-out these rows
for (int j=0; j<M[e].size(); j++)
M[e][j] = 0;
}
for (auto& e:usetCols) { // zero-out these cols
for (int i=0; i<M.size(); i++)
M[i][e] = 0;
}
}
// print the resultant matrix
for (i=0; i<M.size(); i++) {
for (j=0; j<M[i].size(); j++) {
cout << M[i][j] << " ";
}
cout << "\n";
}
}