given a 2d matrix, scan the matrix if a[i][j] is 0, set I row and j col as 0, in place and linear time
scan the matrix, if you get a 0 at a[i][j], use I row and j col as hash table to record the row and col which should be set as 0
foo(int** a, int n)
{
int x=-1;
int y=-1;
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(a[i][j]==0&&x=-1&&y=-1)
{ x=i;y=j;}
else if(a[i][j]==0)
{a[x][j]=0;a[i][y]=0;}
}
for(int j=0;j<n;j++)
{
if(j!=y && a[x][j]==0)
set all a[][j] as 0;
}
for(i!=x&& a[i][y]==0)
{
if(i!=x&& a[i][y]==0)
set all a[i][] as 0
}
set all a[x][] as 0;
aet all a[][y] as 0;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment