Easiest C++ solution | Full marks


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

int rows=A.size();
int columns = A[0].size();

unordered_set<int> r;
unordered_set<int> c;

int i,j,k,row,column;

for(i=0; i<rows; i++)
    for(j=0;j<columns; j++)
        if(A[i][j]==0)      { r.insert(i); c.insert(j); }
        
for(auto it=r.begin(); it!=r.end(); it++)
    for(j=0; j<columns; j++)  
        A[*it][j]=0;
        
for(auto it=c.begin(); it!=c.end(); it++)
    for(i=0; i<rows; i++)   
        A[i][*it]=0;

}


#2

Why is this approach faster than doing it with a vector even tho accessing a vector by index happens in true O(1) time?