-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetmatrixzero.cpp
80 lines (79 loc) · 908 Bytes
/
setmatrixzero.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
void Solution::setZeroes(vector<vector > &A) {
int n=A.size();
int m=A[0].size();
int zerothRows=1;
int zerothCols=1;
if(A[0][0]==0)
{
zerothRows=0;
zerothCols=0;
A[0][0]=1;
}
for(int i=0;i<=n-1;i++)
{
if(A[i][0]==0)
{
zerothCols=0;
}
}
for(int j=0;j<=m-1;j++)
{
if(A[0][j]==0)
{
zerothRows=0;
}
}
for(int i=1;i<=n-1;i++)
{
for(int j=1;j<=m-1;j++)
{
if(A[i][j]==0)
{
A[0][j]=0;
A[i][0]=0;
}
}
}
for(int i=0;i<=n-1;i++)
{
if(A[i][0]==0)
{
int j=0;
while(j<=m-1)
{
A[i][j]=0;
j++;
}
}
}
for(int j=0;j<=m-1;j++)
{
if(A[0][j]==0)
{
int i=0;
while(i<=n-1)
{
A[i][j]=0;
i++;
}
}
}
if(zerothRows==0)
{
int j=0;
while(j<=m-1)
{
A[0][j]=0;
j++;
}
}
if(zerothCols==0)
{
int i=0;
while(i<=n-1)
{
A[i][0]=0;
i++;
}
}
}