leetcode——73.矩阵置零

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

posted @ 2020-07-10 17:47  欣姐姐  阅读(158)  评论(0编辑  收藏  举报