【leetcode】73. Set Matrix Zeroes【java】

LeetCode 73. 矩阵置零(java实现) 阅读详情

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

click to show follow up.

Follow up:

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.

Could you devise a constant space solution?


Analysis

This problem should be solved in place, i.e., no other array should be used. We can use the first column and the first row to track if a row/column should be set to 0.

Since we used the first row and first column to mark the zero row/column, the original values are changed.

Specifically, given, the following matrix
set-matrix-zero-1
this problem can be solved by following 4 steps:

Step 1:
First row contains zero = true;
First column contains zero = false;

Step 2: use first row and column to mark zero row and column.
set-matrix-zero-2

Step 3: set each elements by using marks in first row and column.
set-matrix-zero-3

Step 4: Set first column and row by using marks in step 1.
set-matrix-zero-4

Java Solution

public class Solution {
    public void setZeroes(int[][] matrix) {
        int m = matrix.length;
        int n = matrix[0].length;
        Boolean first_row = false;
        Boolean first_col = false;
        //set first row and column zero or not
        for (int i = 0; i < m; i++){
            if (matrix[i][0] == 0){
                first_col = true;
                break;
            }
        }
        for (int j = 0; j < n; j++){
            if (matrix[0][j] == 0){
                first_row = true;
                break;
            }
            
        }
        //mark zeros on first row and column
        for (int i = 1; i < m; i++){
            for (int j = 1; j < n; j++){
                if (matrix[i][j] == 0){
                    matrix[0][j] = 0;
                    matrix[i][0] = 0;
                }
            }
        }
        //use mark to set elements
        for (int i = 1; i < m ; i++){
            if (matrix[i][0] == 0){
                for (int j = 1; j < n; j++){
                    matrix[i][j] = 0;
                }
            }
        }
        for (int j = 1; j < n ; j++){
            if (matrix[0][j] == 0){
                for (int i = 1; i < m; i++){
                    matrix[i][j] = 0;
                }
            }
        }
        //set first column and row
        if (first_row){
            for (int j = 0; j < n; j++){
                matrix[0][j] = 0;
            }
        }
        if (first_col){
            for (int i = 0; i < m; i++){
                matrix[i][0] = 0;
            }
        }
    }
}


用Seurat和ComplexHeatmap绘制单细胞marker基因热图:从数据处理到可视化全流程 本文详细介绍了使用Seurat和ComplexHeatmap绘制单细胞marker基因热图的全流程,涵盖从数据处理、基因筛选、平均表达矩阵计算到高级可视化与注释添加。重点解析了如何利用ComplexHeatmap生成发表级热图,并整合热词信息,解决单细胞数据可视化中的常见问题,提升科研图表质量。 阅读详情

相关推荐

安川机器人SOCKET 通讯

命令 功能 参数 示例 返回 SelectJob 选择程序 JobName LineNo SelectJob TEST 0 #SelectJob,Ok StartJob 启动程序 JobName StartJob TEST #StartJob,Ok Pause 暂停当前程序 - Pause #Pause,Ok Continue 继续当前程序 - Continue #Continue,Ok ServoOn 伺服上电 - ServoOn #ServoOn,Ok ServoOf...

记录学习内容 8288

leetcode第73题,矩阵置零,java实现

题目链接 运行结果 执行结果:通过 执行用时 :2 ms, 在所有 Java 提交中击败了55.24%的用户 内存消耗 :47.8 MB, 在所有 Java 提交中击败了87.63%的用户 代码与注释 class Solution { public void setZeroes(int[][] matrix) { int R = matrix.length; // 行长度...

qq_29168809的博客 294

【Flow3D实战】从零构建水利冲淤模型:关键步骤与参数详解

本文详细介绍了使用Flow3D构建水利冲淤模型的关键步骤与参数设置技巧。从基础认知到前处理、计算设置和后处理,全面解析如何高效模拟水流与泥沙的相互作用,特别适用于桥墩冲刷、水库泄洪等工程场景。通过实战案例分享,帮助读者掌握Flow3D在水利工程中的精准应用,提升模型精度与计算效率。

weixin_42549723的博客 513

LeetCode 73矩阵置零(java)

class Solution { public void setZeroes(int[][] matrix) { int m = matrix.length, n = matrix[0].length; int [][] temp = new int [m][n]; for(int i = 0; i < m; ++i) for(int j = 0; j < n; ++j) .

weixin_43170297的博客 218

通信原理与MATLAB(八):2PSK的调制解调

通信原理和MATLAB

菜yuan~的博客 3万+

FANUC机器人通过ROBOGUIDE实现与实际的机器人进行程序导入导出的具体方法示例

FANUC机器人通过ROBOGUIDE实现与实际的机器人进行程序导入导出的具体方法示例

Robot_PLC_HMI_Automation 6548

[LeetCode]73. 矩阵置零(java实现)模拟题

[LeetCode]73. 矩阵置零(java实现)模拟题1. 题目2. 读题(需要重点注意的东西)3. 解法4. 可能有帮助的前置习题5. 所用到的数据结构与算法思想6. 总结 1. 题目 2. 读题(需要重点注意的东西) 思路: 要求使用原地算法,即不开辟新的空间; 我们原本需要两个数组来记录在行方向上哪些行需要刷成0,在列方向上哪些列需要刷成0;由于不能开辟额外的空间,我们可以借用原本的矩阵来存储这些信息,即用第0列和第0行来存储哪些行与哪些列需要刷成0,将要刷成0的行和列的值置为0。 这样做,

cloud的博客 539

使用 MATLAB/Simulink + Simscape Electrical + SimPowerSystems + Stateflow,构建一个多能互补微电网系统的动态响应仿真模型

本文详细介绍了使用MATLAB/Simulink构建多能互补微电网动态响应仿真模型的方法。文章从系统架构设计入手,包含光伏、风电、柴油发电机、锂电池储能等多种组件,重点讲解了建模过程、控制策略(下垂控制、PLL、SOC管理)和模式切换逻辑(Stateflow)。通过四种典型场景(负荷突增、光伏出力骤降、并网/孤岛切换、电动机启动)的仿真分析,验证了微电网在动态响应中的关键性能指标,并提出了高可靠性设计建议。该模型为微电网的稳定性分析和优化设计提供了有效工具,特别强调了储能系统在快速功率支撑中的重要作用。

amy_mhd的博客 161

js 矩阵置零

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 示例 1: 输入:matrix = [[1,1,1],[1,0,1],[1,1,1]] 输出:[[1,0,1],[0,0,0],[1,0,1]] 来源:力扣(LeetCode) /** * @param {number[][]} matrix * @return {void} Do not return anything, modify matrix in-place instead.

Cupid 500

小红书图文笔记图像批量爬取:Python爬虫实战与反反爬策略全解析

在当今社交媒体盛行的时代,小红书作为中国领先的生活方式分享平台,积累了海量的优质图文内容。对于数据分析师、市场研究人员或内容创作者来说,能够高效获取这些数据具有重要意义。本文将详细介绍如何使用Python构建一个高效、稳定的小红书图文笔记图像批量爬取工具,涵盖最新技术栈和反反爬策略。本文详细介绍了如何使用Python构建一个高效、稳定的小红书图文笔记图像批量爬取系统。通过结合Playwright、aiohttp等现代工具,实现了从列表页采集到图片下载的完整流程。

2201_76125261的博客 2366

AI原生SaaS应用的自动化运维体系构建

随着ChatGPT掀起的AI原生应用浪潮,越来越多SaaS(软件即服务)产品开始以"AI优先"的方式设计——从用户交互到核心功能,都深度依赖机器学习模型(如推荐算法、智能客服)、实时数据流(如用户行为分析)和弹性算力(如大模型推理)。这类应用的运维需求与传统SaaS完全不同:传统运维像"修水管",哪里漏了补哪里;AI原生运维则像"照顾会成长的宠物",需要实时感知模型状态、数据变化和流量波动,主动调整资源。

AI天才研究院 1082

LeetCode 73 — Set Matrix Zeroes(C++ Java Python)

题目:http://oj.leetcode.com/problems/set-matrix-zeroes/ Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straig

龙之梦 3192

一文入门车载以太网:从原理到实战,看完就能上手开发

新手选型遵循“适配场景、兼顾成本、优先车规”的原则:普通场景(车载娱乐、普通诊断):100Base-T1 PHY(LAN8742)+ 非TSN交换机(SJA1105)+ STM32H743 ECU + 普通STP线缆;ADAS场景:1000Base-T1 PHY(TJA1101)+ TSN交换机(SJA1110)+ NXP S32G399 ECU + 高抗干扰STP线缆;避免过度选型:如普通娱乐场景无需选择TSN交换机、ASIL-B级芯片,否则会增加成本;

getusushu的博客 320

【Java】【LeetCode】73. Set Matrix Zeroes

题目: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do itin-place. Example 1: Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ...

zhelong19830726的博客 147

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

[LeetCode] 73. Set Matrix Zeroes Java

题目: iven amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space?A straight forward solution using ...

AC616288149的博客 166

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【LeetCode】 73. Set Matrix Zeroes 矩阵置零(Medium)(JAVA)

【LeetCode】 73. Set Matrix Zeroes 矩阵置零(Medium)(JAVA) 题目地址: https://leetcode.com/problems/set-matrix-zeroes/ 题目描述: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-...

qq_16927853的博客 199

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

[LeetCode] 73. Set Matrix Zeroes java

/**73. Set Matrix Zeroes * @param matrix * 二维数组中含有0,则行和列均变成0 */ public void setZeroes(int[][] matrix) { Set<Integer> row = new HashSet(); Set<Integer> col = new HashS

橙煦媛的博客 337

Leetcode 73. Set Matrix Zeroes [medium][java]

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example Solution 1: brute force Consideration Use a boolean matrix to mark if the element has been set t...

inkhk的博客 339

Leetcode 73. Set Matrix Zeroes Java版

解题思路:这道题很容易上手,是典型的矩阵操作问题,但是其中的陷阱是,我们遇到0的时候不能直接把矩阵的行列在当前矩阵直接置0,否则后面还没访问到的会被当成原来是0,最后会把很多不该置0的行列都置0了。我们看到其实判断某一项是不是0只要看它对应的行或者列应不应该置0就可以,所以我们可以维护一个行和列的判断数组,然后扫描一遍矩阵记录那一行或者列是不是应该置0即可,后面赋值是一个常量时间的判断。这种方法的...

qq_28900249的博客 210

【LeetCode-Java实现】73. Set Matrix Zeroes矩阵置0

73. Set Matrix Zeroes矩阵置0题目描述思路实现 题目描述 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], ...

IT Crowd的博客 181

73. Set Matrix Zeroes-leetcode-java

【原来在SAE的blog上,都转到优快云了。。】 73. Set Matrix Zeroes-leetcode-java 发表于 2016/02/11 题目 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

you are what you read 678

(java)leetcode-73:Set Matrix Zeroes

Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click to show follow up. Follow up: Did you use extra space? A straight forward s

277

LeetCode in Java [8]: 73. Set Matrix Zeroes*

Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do itin-place. Example 1: Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ] ...

Kuroneko 141

LeetCode 73. Set Matrix Zeroes 矩阵置零(Java)

题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Example 1: Input: [ [1,1,1], [1,0,1], [1,1,1] ] Output: [ [1,0,1], [0,0,0], [1,0,1] ] Example 2: In...

qq_41878358的博客 211

【LeetCode】No.73. Set Matrix Zeroes -- Java Version

【LeetCode】No.73. Set Matrix Zeroes -- Java Version

汤姆lazy的优快云博客 778

LeetCode(73):矩阵置零 Set Matrix Zeroes(Java)

2019.6.2 #程序员笔试必备# LeetCode 从零单刷个人笔记整理(持续更新) 难得的限定空间复杂度的题目。 题目难点在于不能让修改值影响遍历检查,如果检查到0值的时候,直接将本行本列置零,则会影响后续遍历;如果检查到0值的时候,只对该行该列之前位置置零,则会产生大量重复。因此必须要开辟空间对行列的检查情况进行存储。 如何只用常量空间存储行列检查情况呢?可以利用矩阵的首行存储各列情况,...

NJU_ChopinXBP的博客 284
上一篇: 【leetcode】72. Edit Distance【java】
下一篇: 【leetcode】74. Search a 2D Matrix【java】
陈善亮-BUPT
博客等级 码龄11年 40粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值