-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsolution_linear_space.cpp
49 lines (44 loc) · 1.15 KB
/
solution_linear_space.cpp
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
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
void setZeroes(vector<vector<int>>& matrix) {
int rows = matrix.size();
if (rows == 0) {
return;
}
int cols = matrix[0].size();
vector<int> row_zero(rows, 0);
vector<int> col_zero(cols, 0);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == 0) {
row_zero[i] = 1;
col_zero[j] = 1;
}
}
}
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (row_zero[i] || col_zero[j]) {
matrix[i][j] = 0;
}
}
}
}
};
int main () {
Solution solver;
vector<vector<int> > matrix = {{0, 1, 2, 0},
{3, 4, 5, 2},
{1, 3, 1, 5}};
solver.setZeroes(matrix);
for (auto row: matrix) {
for (auto x: row) {
cout << x << " ";
}
cout << endl;
}
return 0;
}