simple solution using queue for row, col


  • 0
    P
    void setZeroes(vector<vector<int>>& matrix) {
        
        queue<int> row;
        queue<int> col;
        
        for( int i = 0 ; i < matrix.size() ; i++ ) {
            for( int j = 0 ; j < matrix.at(i).size() ; j++ ) {
                if( matrix.at(i).at(j) == 0 ) {
                    row.push(i);
                    col.push(j);
                }
            }
        }
        
        while( row.size() > 0 ) {
            for( int i = 0 ; i < matrix.at(row.front()).size() ; i++ ) {
                matrix.at(row.front()).at(i) = 0;
            }
            row.pop();
        }
        
        while( col.size() > 0 ) {
            for( int i = 0 ; i < matrix.size() ; i++ ) {
                matrix.at(i).at(col.front()) = 0;
            }
            col.pop();
        }
        
        return;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.