3
\$\begingroup\$

How is my implementation? Are there improvements I can make to the variable names or any edge cases I might have missed?

public class ZeroMatrix {
    public static void main(String[] args) {
        int[][] matrix = new int[][]{
                {1, 0, 2},
                {3, 4, 1}
        };
        System.out.println(Arrays.deepToString(setZeroes(matrix)));
    }

    private static int[][] setZeroes(int[][] matrix) {
        boolean[] rowsWithZero = new boolean[matrix.length];
        boolean[] columnsWithZero = new boolean[matrix[0].length];

        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[0].length; j++) {
                if (matrix[i][j] == 0) {
                    rowsWithZero[i] = true;
                    columnsWithZero[j] = true;
                }
            }
        }
        // Iterate through the rowsWithZero array and transform all rows with 0 to 0
        for (int i = 0; i < rowsWithZero.length; i++) {
            if (rowsWithZero[i]) {
                transformRow(i, matrix);
            }
        }

        // Iterate through columnsWithZero and transform all columns with 0 to 0.

        for (int i = 0; i < columnsWithZero.length; i++) {
            if (columnsWithZero[i]) {
                transformColumn(i, matrix);
            }
        }


        return matrix;

    }

    private static void transformColumn(int column, int[][] matrix) {
        for (int row = 0; row < matrix.length; row++) {
            matrix[row][column] = 0;
        }
    }

    private static void transformRow(int row, int[][] matrix) {
        for (int column = 0; column < matrix[0].length; column++) {
            matrix[row][column] = 0;
        }
    }
}
\$\endgroup\$
2
\$\begingroup\$

comments

Comments should say why the code is like it is. Any other information to the reader should be given to the reader by the identifiers to chose.

Your comments structure the method in 3 logical sections (the first without a dedicated comment). You should better move this three sections into separate methods with names derived from the comments:

private static int[][] setZeroes(int[][] matrix) {
    boolean[] rowsWithZero = new boolean[matrix.length];
    boolean[] columnsWithZero = new boolean[matrix[0].length];

    findZeros(matrix,rowsWithZero,columnsWithZero);
    transformColumns(matrix,columnsWithZero);
    transformRows(matrix,rowsWithZero);

    return matrix;
}
private static void findZeros(int[][] matrix, boolean[] rowsWithZero, boolean[] columnsWithZero){
    for (int i = 0; i < matrix.length; i++) {
        for (int j = 0; j < matrix[0].length; j++) {
            if (matrix[i][j] == 0) {
                rowsWithZero[i] = true;
                columnsWithZero[j] = true;
            }
        }
    }
}
private static void transformColumns(int[][] matrix,  boolean[] columnsWithZero){
    for (int i = 0; i < columnsWithZero.length; i++) {
        if (columnsWithZero[i]) {
            transformColumn(i, matrix);
        }
    }
}
private static void transformRows(int[][] matrix, boolean[] rowsWithZero){
    for (int i = 0; i < rowsWithZero.length; i++) {
        if (rowsWithZero[i]) {
            transformRow(i, matrix);
        }
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.