-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
setMatrixZeroes.cpp
84 lines (74 loc) · 2.33 KB
/
setMatrixZeroes.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
// Source : https://oj.leetcode.com/problems/set-matrix-zeroes/
// Author : Hao Chen
// Date : 2014-06-23
/**********************************************************************************
*
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.
*
* click to show follow up.
*
* Follow up:
*
* Did you use extra space?
* A straight forward solution using O(mn) space is probably a bad idea.
* A simple improvement uses O(m + n) space, but still not the best solution.
* Could you devise a constant space solution?
*
*
**********************************************************************************/
class Solution {
public:
Solution(){
srand(time(NULL));
}
void setZeroes(vector<vector<int> > &matrix) {
if(random()%2){
setZeroes1(matrix);
}
setZeroes2(matrix);
}
void setZeroes1(vector<vector<int> > &matrix) {
int bRow = false, bCol=false;
for (int r=0; r<matrix.size(); r++){
for(int c=0; c<matrix[r].size(); c++){
if (matrix[r][c]==0){
if (r==0) bRow = true;
if (c==0) bCol = true;
matrix[0][c] = matrix[r][0] = 0;
}
}
}
for (int r=1; r<matrix.size(); r++){
for(int c=1; c<matrix[r].size(); c++){
if (matrix[0][c]==0 || matrix[r][0]==0) {
matrix[r][c]=0;
}
}
}
if (bRow){
for(int c=0; c<matrix[0].size(); c++) matrix[0][c] = 0;
}
if (bCol){
for(int r=0; r<matrix.size(); r++) matrix[r][0] = 0;
}
}
void setZeroes2(vector<vector<int> > &matrix) {
bool *row = new bool[matrix.size()]();
bool *col = new bool[matrix[0].size()]();
for (int r=0; r<matrix.size(); r++){
for(int c=0; c<matrix[r].size(); c++){
if (matrix[r][c]==0){
row[r]=true;
col[c]=true;
}
}
}
for (int r=0; r<matrix.size(); r++){
for(int c=0; c<matrix[r].size(); c++){
if (row[r] || col[c]) {
matrix[r][c]=0;
}
}
}
}
};