【矩阵】73. 矩阵置零

题目

法1:自己想的笨蛋方法

java 复制代码
class Solution {
    public void setZeroes(int[][] matrix) {
        Set<Integer> rowSet = new HashSet<>();
        Set<Integer> columnSet = new HashSet<>();
        for (int i = 0; i < matrix.length; ++i) {
            for (int j = 0; j < matrix[0].length; ++j) {
                if (matrix[i][j] == 0) {
                    rowSet.add(i);
                    columnSet.add(j);
                }
            }
        }
        for (int i = 0; i < matrix.length; ++i) {
            if (rowSet.contains(i)) {
                Arrays.fill(matrix[i], 0);
            }
        }
        for (int j = 0; j < matrix[0].length; ++j) {
            if (columnSet.contains(j)) {
                for (int i = 0; i < matrix.length; ++i) {
                    matrix[i][j] = 0;
                }
            }
        }
    }
}

// 代码优雅版
class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length;
        boolean[] row = new boolean[m];
        boolean[] col = new boolean[n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (matrix[i][j] == 0) {
                    row[i] = col[j] = true;
                }
            }
        }
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (row[i] || col[j]) {
                    matrix[i][j] = 0;
                }
            }
        }
    }
}

作者:力扣官方题解
链接:https://leetcode.cn/problems/set-matrix-zeroes/solutions/669901/ju-zhen-zhi-ling-by-leetcode-solution-9ll7/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

法2:

放个链接,估计真遇到了也写不出来。。。
https://leetcode.cn/problems/set-matrix-zeroes/solutions/670278/xiang-jie-fen-san-bu-de-o1-kong-jian-jie-dbxd/?envType=study-plan-v2&envId=top-100-liked

相关推荐
AI科技星14 小时前
特征值与特征向量不是矩阵特殊解,是变换矩阵下不改变生长方向、仅缩放体量的固有主螺旋脉络 -《全域数学vs传统数学:人类文明进阶200讲》第73讲
人工智能·线性代数·矩阵·数据挖掘·回归·乖乖数学·全域数学
学究天人16 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷7)
线性代数·矩阵·动态规划·概率论·图论·抽象代数·拓扑学
学究天人18 小时前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷8)
线性代数·算法·机器学习·数学建模·动态规划·图论·抽象代数
USB_ABC20 小时前
视频矩阵核心技术对比:模块化vs固定机箱、硬件帧同步vs软件同步怎么选
矩阵·音视频
workflower2 天前
室内外配送机器人-应用路径
人工智能·机器学习·设计模式·矩阵·自动化
hai3152475432 天前
九章编译法----空间几何统一编译法
网络·汇编·人工智能·线性代数
lin9902122 天前
内容矩阵批量分发实战
大数据·人工智能·矩阵
学究天人2 天前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷3.2)
线性代数·算法·机器学习·数学建模·动态规划·抽象代数·拓扑学
学究天人2 天前
数学公理体系大全:Comprehensive Collection of Mathematical Axiom Systems(卷1)
线性代数·机器学习·数学建模·动态规划·图论·抽象代数·傅立叶分析
C137的本贾尼2 天前
第8章:把向量组织起来——矩阵与线性变换
线性代数·矩阵