C++ solution - accepted


#1
void Solution::setZeroes(vector<vector<int> > &A) {
    // Do not write main() function.
    // Do not read input, instead use the arguments to the function.
    // Do not print the output, instead return values as specified
    // Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

    set<int> row;
    set<int> col;
    
    for(int i = 0; i < A.size(); i++){
        for(int j = 0; j < A[i].size(); j++){
            if(A[i][j] == 0){
                row.insert(i);
                col.insert(j);
            }
        }
    }
    
    for(int i = 0; i < A.size(); i++){
        for(int j = 0; j < A[i].size(); j++){
            if(row.find(i) != row.end() || col.find(j) != col.end()){
                A[i][j] = 0;
            }
        }
    }

}

#2

This solution fails for larger test cases. Do you have any idea why?


#3
    ArrayList<Integer> x = new ArrayList<Integer>();
    ArrayList<Integer> y = new ArrayList<Integer>();
    
    for(int i=0;i<a.size();i++){
        for(int j=0;j<a.get(0).size();j++){
            if(a.get(i).get(j)==0){
                x.add(i);y.add(j);
            }
        }
    }
    
    
     for(int i=0;i<a.size();i++){
        for(int j=0;j<a.get(0).size();j++){
            if(x.contains(i) || y.contains(j)){
            a.get(i).set(j,0); 

            }
        }
    }
    
    My solution is in java and exactly same as yours but mine is partially correct , can you explain reason why its that

#4

I also wrote the same code(similar to chirag-gupta_831) in java and mine is also partially correct. Can someone explain how it could be corrected.


#5

Hi, i was able to find the solution so posting it here:

public class Solution {
public void setZeroes(ArrayList<ArrayList> a) {
int m=a.size();
int n=a.get(0).size();
ArrayList row=new ArrayList();
ArrayList column=new ArrayList();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(a.get(i).get(j)==0)
{
row.add(i);
column.add(j);
}

        }
    }
  for(int i=0;i<m;i++)
    {if(row.contains(i))
        for(int j=0;j<n;j++)
        {
            a.get(i).set(j,0);
        }
        
    }
    for(int j=0;j<n;j++)
    {if(column.contains(j))
        for(int i=0;i<m;i++)
        {
            a.get(i).set(j,0);
        }
        
    }
}

}


#6

we have to do in O(1) extra space

so logic is use first row and first column to store which all row and column we have to make 0
think think !!


#7

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

set<int> row;
set<int> col;

for(int i = 0; i < A.size(); i++){
    for(int j = 0; j < A[i].size(); j++){
        if(A[i][j] == 0){
            row.insert(i);
            col.insert(j);
        }
    }
}

for(int i=0;i<A.size();i++)
{
    if(row.find(i) != row.end())
    {
        for(int j=0;j<A[0].size();j++)
            A[i][j]=0;
    }
}
for(int j=0;j<A[0].size();j++)
{
    if(col.find(j) != col.end())
    {
        for(int i=0;i<A.size();i++)
            A[i][j]=0;
    }
}

}