zoukankan      html  css  js  c++  java
  • 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

    我的前方是万里征途,星辰大海!!
  • 相关阅读:
    BZOJ 3677: [Apio2014]连珠线 树形DP
    TweenMax说明
    vs 中快捷实现父类方法
    Box2d b2World的RayCast方法
    cocos2d-js 帧序列动画
    cocos2d-js 显示帧序列图中的一帧
    不同类型刚体接触测试
    FlashDevelop调试Air出错
    php 创建删除数据库
    本地php 连接 MySQL
  • 原文地址:https://www.cnblogs.com/taoyuxin/p/13280496.html
Copyright © 2011-2022 走看看