首页 > 其他 > 详细

零矩阵问题

时间:2021-05-21 22:24:10      阅读:27      评论:0      收藏:0      [点我收藏+]

问题:

技术分享图片

 

class Solution {
    public void setZeroes(int[][] matrix) {
        boolean[] row = new boolean[matrix.length];
        boolean[] column = 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) {
                    row[i] = true;
                    column[j] = true;
                }
            }
        }
        for(int i=0;i<matrix.length;i++) {
            for(int j=0;j<matrix[0].length;j++) {
                if(row[i]||column[j]) {
                    matrix[i][j] = 0;
                }
            }
        }
    }
}

 

定义boolean型数组row,column。遍历二维数组,将值为0的项的行和列赋值给布尔数组。再次遍历数组,只要行值或列值有一个为true,那么就将具体值matrix[i][j]置0;

 

零矩阵问题

原文:https://www.cnblogs.com/HanLue/p/14794526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!