01矩阵相关

1.将01矩阵中,为0位置所在的行和列置为0

时间复杂度:O(m*n)

一行一行扫描,记录记录其中为0的列号

public void setZeroes(int[][] matrix) {
        boolean[] flag = new boolean[matrix[0].length];
        for(int i =0; i<matrix.length; i++) {
            boolean contains0 = false;
            for (int j =0; j < matrix[0].length; j++) {
                if(matrix[i][j] == 0) {
                    contains0 = true;
                    flag[j] = true;
                }
            }
            if(contains0)
                for(int j=0; j<matrix[0].length; j++) matrix[i][j] = 0;
        }
        for(int j = 0; j < matrix[0].length; j++)
            if(flag[j])
                for(int i =0; i<matrix.length; i++) matrix[i][j] = 0;
}

 

posted @ 2019-09-13 10:08  LeeJuly  阅读(127)  评论(0)    收藏  举报