C++ Simple Solution Time Complexity O(n*m) and Space Complexity O(1)


#1

void Solution::setZeroes(vector<vector > &A) {

signed int n=A.size();
if(n<=0)
return ;
signed int m=A[0].size();

//row major
for(int i=0;i<n;i++)
{
    int cnt=0;
    for(int j=0;j<m;j++)
    {
        if(A[i][j]==0)
        {
            cnt=1;
            break;
        }
    }
    if(cnt)
    {
        for(int j=0;j<m;j++)
        {
            if(A[i][j]==1)
            {
                A[i][j]=2;  
                
                /* if cnt is greater than 0 than this column is 
                being set to two temporarily but after 2nd transversal 
                they will be set to 0 
                */
            }
        }
    }
}
// col major
for(int i=0;i<m;i++)
{
    int cnt=0;
    for(int j=0;j<n;j++)
    {
        if(A[j][i]==0)
        {
            cnt=1;
            break;
        }
        if(A[j][i]==2)
        A[j][i]=0;
        /* 
                2nd transversal temporary 2 set to 0 
        */
    }
    if(cnt)
    {
        for(int j=0;j<n;j++)
        
            A[j][i]=0;
            /* 
            if cnt is greater than 0 than this row is set to 0 
            */
            
    }
}

return ;

}