-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet Matrix Zeroes.txt
40 lines (40 loc) · 1.27 KB
/
Set Matrix Zeroes.txt
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
class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int nrows = matrix.size();
if (nrows == 0)
return;
int ncols = matrix[0].size();
if (ncols == 0)
return;
bool row = true, col = true;
for (int i = 0; (i < nrows) && row; i++)
if (!matrix[i][0])
row = false;
for (int j = 0; (j < ncols) && col; j++)
if (!matrix[0][j])
col = false;
for (int i = 1; i < nrows; i++)
for (int j = 1; j < ncols; j++)
if (!matrix[i][j]) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
for (int i = 1; i < nrows; i++) {
if (!matrix[i][0])
for (int j = 1; j < ncols; j++)
matrix[i][j] = 0;
}
for (int j = 1; j < ncols; j++) {
if (!matrix[0][j])
for (int i = 1; i < nrows; i++)
matrix[i][j] = 0;
}
for (int i = 0; (i < nrows) && !row; i++)
matrix[i][0] = 0;
for (int j = 0; (j < ncols) && !col; j++)
matrix[0][j] = 0;
}
};