-
Notifications
You must be signed in to change notification settings - Fork 5
/
Solution.cpp
84 lines (77 loc) · 2.29 KB
/
Solution.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
using namespace std;
// 5.9 ratio
class Solution {
public:
// straight forward version
void setZeroes1(vector<vector<int>>& matrix) {
int m = matrix.size();
if (m == 0) return;
int n = matrix[0].size();
vector<vector<int>> mx = matrix;
for (int i=0; i<m; i++) {
for (int j=0; j<n; j++) {
if (mx[i][j] == 0) {
for (int i=0; i<m; i++) {
matrix[i][j] = 0;
}
for (int j=0; j<n; j++) {
matrix[i][j] = 0;
}
}
}
}
}
// straight forward version
void setZeroes2(vector<vector<int>>& matrix) {
vector<bool> icz (matrix.size(), false);
if (icz.size() == 0) return;
vector<bool> jcz (matrix[0].size(), false);
for (int i=0; i<matrix.size(); i++) {
for (int j=0; j<matrix[0].size(); j++) {
if ( matrix[i][j] == 0 ) {
icz[i] = true;
jcz[j] = true;
}
}
}
for (int i=0; i<matrix.size(); i++) {
for (int j=0; j<matrix[0].size(); j++) {
if ( icz[i] or jcz[j] ) {
matrix[i][j] = 0;
}
}
}
}
// constant space
void setZeroes(vector<vector<int>>& matrix) {
bool frow = false;
for (int i=0; i<matrix.size(); i++) {
for (int j=0; j<matrix[0].size(); j++) {
if ( matrix[i][j] == 0 ) {
if (i == 0) frow = true;
else matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
for (int i=1; i<matrix.size(); i++) {
for (int j=1; j<matrix[0].size(); j++) {
if (matrix[i][0] == 0 or matrix[0][j] == 0)
matrix[i][j] = 0;
}
}
if (matrix[0][0] == 0)
for (int i=0; i<matrix.size(); i++)
matrix[i][0] = 0;
if (frow)
for (int j=0; j<matrix[0].size(); j++)
matrix[0][j] = 0;
}
};
int main() {
Solution a;
}