首页 > 其他 > 详细

leetcode——73.矩阵置零

时间:2020-07-10 20:29:21      阅读:45      评论:0      收藏:0      [点我收藏+]
public void setZeroes(int[][] matrix) {
        //遍历矩阵,标记要置零的行列,之后进行置零。
        ArrayList<Integer> row = new ArrayList<>();
        ArrayList<Integer> col = new ArrayList<>();
        int m = matrix.length;
        int n = matrix[0].length;
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(matrix[i][j] == 0){
                    if(!row.contains(i)){
                        row.add(i);
                    }
                    if(!col.contains(j)){
                        col.add(j);
                    }
                }
            }
        }
        //找出了需要置零的行列
        for(int i = 0;i<m;i++){
            if(row.contains(i)){
                for(int j = 0;j<n;j++){
                    matrix[i][j] = 0;
                }
            }
        }
        for(int j = 0;j<n;j++){
            if(col.contains(j)){
                for(int i = 0;i<m;i++){
                    matrix[i][j] = 0;
                }
            }
        }
    }

技术分享图片

 

 

 

public void setZeroes(int[][] matrix) {
        //遍历矩阵,标记要置零的行列,之后进行置零。
        ArrayList<Integer> row = new ArrayList<>();
        ArrayList<Integer> col = new ArrayList<>();
        int m = matrix.length;
        int n = matrix[0].length;
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(matrix[i][j] == 0){
                    if(!row.contains(i)){
                        row.add(i);
                    }
                    if(!col.contains(j)){
                        col.add(j);
                    }
                }
            }
        }
        //找出了需要置零的行列
        for(int i = 0;i<m;i++){
            for(int j = 0;j<n;j++){
                if(row.contains(i) || col.contains(j)) {
                    matrix[i][j] = 0;
                }
            }
        }
    }

技术分享图片

 

 

——2020.7.10

leetcode——73.矩阵置零

原文:https://www.cnblogs.com/taoyuxin/p/13280496.html

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