Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are set to 0.

There is a same problem in leetcode. We could use the first row and first colown to record each row or colown has zero or not. Since we would change the value of first row and first colown, we use two additional boolean var to record whether they contain zero or not. The time complexity would be O(MN) and the space complexity would be O(1).

/* Use the first row and first colown to record each row's and colown's condition. But first use
two additional boolean var to record the condition of first row and colown, since we would change 
their value. The time complexity would be O(M*N), the space complexity is O(1)

*/
public class ZeroMatrix {
    public void zeroMatrix(int[][] matrix) {
        int m = matrix.length; if(m == 0) return;
        int n = matrix[0].length; if(n== 0) return;

        boolean r_zero = false, c_zero = false;
        for(int i = 0; i < m; i++) {
            if(matrix[i][0] == 0) {
                c_zero = true; break;
            }
        }
        for(int i = 0; i < n; i++) {
            if(matrix[0][i] == 0) {
                r_zero = true; break;
            }
        }

        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                if(matrix[i][j] == 0) {
                    matrix[0][j] = 0;
                    matrix[i][0] = 0;
                }
            }
        }

        for(int i = 1; i < m; i++) {
            if(matrix[i][0] == 0) {
                for(int j = 0; j < n; j++) 
                    matrix[i][j] = 0;
            }
        }

        for(int i = 1; i < n; i++) {
            if(matrix[0][i] == 0) {
                for(int j = 0; j < m; j++)
                    matrix[j][i] = 0;
            }
        }

        if(r_zero == true) {
            for(int i = 0; i < n; i++)
                matrix[0][i] = 0;
        }

        if(c_zero == true) {
            for(int i = 0; i < m; i++)
                matrix[i][0] = 0;
        }
    }

    public void print(int[][] matrix) {
        int n = matrix.length; 
        if(n == 0) return;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < n; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println("");
        }
        System.out.println("");
    }

    public static void main(String[] args) {
        ZeroMatrix zm = new ZeroMatrix();
        int[][] matrix1 = {{0, 1, 1}, {1, 1, 1}, {1, 1, 1}};
        int[][] matrix2 = {{1, 1, 1}, {0, 1, 1}, {1, 1, 1}};
        int[][] matrix3 = {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}};
        int[][] matrix4 = {{1, 0, 1}, {1, 1, 1}, {1, 1, 1}};
        int[][] matrix5 = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}};
        int[][] matrix6 = {{1, 1, 1}, {1, 1, 1}, {1, 1, 0}};
        zm.zeroMatrix(matrix1);
        zm.print(matrix1);
        zm.zeroMatrix(matrix2);
        zm.print(matrix2);
        zm.zeroMatrix(matrix3);
        zm.print(matrix3);
        zm.zeroMatrix(matrix4);
        zm.print(matrix4);
        zm.zeroMatrix(matrix5);
        zm.print(matrix5);
        zm.zeroMatrix(matrix6);
        zm.print(matrix6);
    }
}