CTCI 1.7

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);
    }
}

 

转载于:https://www.cnblogs.com/pyemma/p/3828439.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值