forked from Ritesh25696/interviewBit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet Matrix Zeros
46 lines (39 loc) · 1.05 KB
/
Set Matrix Zeros
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
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.
***************************************************************************************************************
void Solution::setZeroes(vector<vector<int> > &A) {
map<int , int> row;
map<int , int> col;
for(int i=0 ; i<A.size() ; i++){
for(int j=0 ;j<A[0].size() ;j++){
if(A[i][j] == 0){
row[i] = 1;
col[j] = 1;
}
}
}
for(int i=0 ; i<A.size() ; i++){
if(row[i] == 1){
for(int j = 0 ; j <A[0].size() ; j++){
A[i][j] = 0;
}
}
else{
for(int j=0 ; j<A[0].size(); j++){
if(col[j] == 1){
A[i][j] = 0;
}
}
}
}
}