Set every cell in matrix to 0 if that row or column contains a 0

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

This problem should be solved in place, i.e., no other array should be used. We can use the first column and the first row to track if a row/column should be set to 0. The idea is to store the required information before changing any values in the matrix since we want to do it in place. We are using first row and first column to store the information for existence of zero in all rows and columns respectively.

public class Solution {
    public void setZeroes(int[][] matrix) {
        int x=matrix.length;
        if(x==0) return;
        int y=matrix[0].length;
        
        boolean firstRow = scanRow(matrix,0);
        boolean firstCol = scanCol(matrix,0);
        
       
        for(int i=1; i<x;i++){
            if(scanRow(matrix,i)==true){
                matrix[i][0]=0;
            }
        }
        for(int i=1; i<y;i++){
            if(scanCol(matrix,i) == true){
                matrix[0][i] = 0;
            }
        }
        for(int i=1; i<x;i++){
            for(int j=1; j<y;j++){
                if(matrix[i][0]==0|| matrix[0][j]==0){
                    matrix[i][j]=0;
                }
            }
        }
        if(firstRow ==true){
            for(int i=0; i<y;i++){
                matrix[0][i]=0;
            }
        }
        if(firstCol==true){
            for(int i=0; i<x;i++){
                matrix[i][0]=0;
            }
        }
    }
    
    public boolean scanRow(int[][] matrix, int row_num){
        for(int i=0; i<matrix[0].length;i++){
            if(matrix[row_num][i] == 0){
                return true;
            }
        }
        return false;
    }
    
    public boolean scanCol(int[][] matrix, int col_num){
        for(int i=0;i<matrix.length;i++){
            if(matrix[i][col_num]==0){
                return true;
            }
        }
        return false;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation