-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetMatrixZeros.cpp
More file actions
59 lines (46 loc) · 1.27 KB
/
SetMatrixZeros.cpp
File metadata and controls
59 lines (46 loc) · 1.27 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
/*
Given an m x n matrix of 0s and 1s, if an element is 0, set its entire row and column to 0.
Do it in place.
Example
Given array A as
1 0 1
1 1 1
1 1 1
On returning, the array A should be :
0 0 0
1 0 1
1 0 1
Note that this will be evaluated on the extra memory used. Try to minimize the space and time complexity.
LINK: https://www.interviewbit.com/problems/set-matrix-zeros/
*/
void Solution::setZeroes(vector<vector<int> > &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details
int m = A.size();
int n = A[0].size();
vector<bool> r(m, false), c(n, false);
for(int i = 0; i< m; i++){
for(int j =0; j<n; j++){
if(A[i][j] == 0){
r[i] = true;
c[j] = true;
}
}
}
for(int i = 0; i< m; i++){
if(r[i]){
for(int j =0; j<n; j++){
A[i][j] = 0;
}
}
}
for(int j = 0; j< n; j++){
if(c[j]){
for(int i =0; i<m; i++){
A[i][j] = 0;
}
}
}
}