Skip to content

Commit 726df5c

Browse files
Merge pull request #4 from sumeet2-lab/setmatrixzero
Leetcode 73 set matrix zero
2 parents 78ed6cc + 03b0943 commit 726df5c

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

73-setmatrixzero.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public void setZeroes(int[][] matrix) {
3+
int row=matrix.length;
4+
int col=matrix[0].length;
5+
int count=0;
6+
7+
for(int i=0;i<row;i++)
8+
{
9+
for(int j=0;j<col;j++)
10+
{
11+
if(matrix[i][j]!=0)
12+
{
13+
count++;
14+
}
15+
}
16+
}
17+
18+
if(count==row*col)
19+
{
20+
return;
21+
}
22+
23+
for(int i=0;i<row;i++)
24+
{
25+
for(int j=0;j<col;j++)
26+
{
27+
if(matrix[i][j]==0)
28+
{
29+
for(int f=i-1;f>=0;f--)
30+
{
31+
if(matrix[f][j]!=0)
32+
matrix[f][j]=-1;
33+
}
34+
for(int f=i+1;f<row;f++)
35+
{
36+
if(matrix[f][j]!=0)
37+
matrix[f][j]=-1;
38+
}
39+
for(int f=j-1;f>=0;f--)
40+
{if(matrix[i][f]!=0)
41+
matrix[i][f]=-1;
42+
}
43+
for(int f=j+1;f<col;f++)
44+
{ if(matrix[i][f]!=0)
45+
matrix[i][f]=-1;
46+
}
47+
}
48+
}
49+
}
50+
51+
52+
for(int i=0;i<row;i++)
53+
{
54+
for(int j=0;j<col;j++)
55+
{
56+
if(matrix[i][j]==-1)
57+
{
58+
matrix[i][j]=0;
59+
}
60+
}
61+
}
62+
63+
return;
64+
}
65+
}

0 commit comments

Comments
 (0)