【矩阵】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

相关推荐
西西弗Sisyphus3 小时前
低秩分解的本质是通过基矩阵和系数矩阵的线性组合,以最小的存储和计算代价近似表示复杂矩阵
线性代数·算法·矩阵
远方160916 小时前
40-Oracle 23 ai Bigfile~Smallfile-Basicfile~Securefile矩阵对比
数据库·人工智能·sql·oracle·矩阵·database
写代码的橘子n1 天前
两个矩阵的卷积运算
矩阵·卷积神经网络
余=185381628002 天前
矩阵混剪系统源码搭建全流程技术解析,矩阵OEM
线性代数·矩阵
蒙奇D索大2 天前
【数据结构】图论最短路圣器:Floyd算法如何用双矩阵征服负权图?
数据结构·算法·矩阵·图论·图搜索算法
IC 见路不走2 天前
LeetCode 第73题:矩阵置零
算法·leetcode·矩阵
jieshenai2 天前
torch 高维矩阵乘法分析,一文说透
pytorch·深度学习·矩阵
引量AI3 天前
技术赋能——AI社媒矩阵营销工具如何重构社媒矩阵底层架构
人工智能·矩阵·自动化·tiktok矩阵·海外社媒
云云3214 天前
亚矩阵云手机针对AdMob广告平台怎么进行多账号的广告风控
大数据·网络·线性代数·游戏·智能手机·矩阵
余=185381628004 天前
矩阵系统源码开发技术难题与应对策略
线性代数·矩阵