LeetCode 73. Set Matrix Zeroes(Medium)

1998年高教社杯全国大学生数学建模竞赛 B 题:《灾情巡视路线》真题解析与 MATLAB 解决方案 🏆 本文已收录于专栏:《滚雪球学数学建模(含历年真题)》,持续更新最新及历年数学建模竞赛资料,每篇内容力求做到“问题讲透、模型讲清、代码可用、论文可参考”,帮助你从零开始逐步建立完整的数学建模知识体系。 阅读详情

原文出处,请点击彼得攀的小站
查阅更多的题解,请点击

Problem

73. Set Matrix Zeroes(Medium)

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:

Input: 
[
    [0,1,2,0],
    [3,4,5,2],
    [1,3,1,5]
]
Output: 
[
    [0,0,0,0],
    [0,4,5,0],
    [0,3,1,0]
]

Follow up:

  • 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?

Solution

O(mn) time, O(1) space

这道题题意很清楚:矩阵中零元素所在的行和列全置为0,希望算法只用常数的额外存储空间。

对于该问题,首先要弄清楚为何需要额外的存储空间:用来存储遍历矩阵时零元素的行号和列号

比较常规的方法是用数组存储,在最坏情况下是O(n+m)space.有两种比较常规的优化思路:

  • 用一个变量尽可能多的表示所需表示的数据:比如可以用两个二进制数,第i位为0,表示第i行或者i列需置0
  • 用原来的空间存储需要记录的数据,这样方式需要覆盖原来的数据,则需该区域数据已经“无用”

第一种方法实现较为麻烦,因为m和n的长度可能过大,单个二进制无法表示。
第二种方法比较合适:

  • 对于查找到的第一个0元素,可以用该元素所在行(记为第i行)存储信息
    若其后某行(记为k,k>i)存在0元素(列号为j),则设matrix[i-1][j-1]$,在该行结束后,将该行置为0
    当遍历结束,第i行中所有0元素的列号标识了所有需要置0的列,将其全置为0后,将第i行也置为0,则结束

GitHub传送门

class Solution
{
  public:
    void setZeroes(vector<vector<int>> &matrix)
    {
        int m = matrix.size();
        int n = matrix[0].size();
        int posOfFirstZeroRow = -1;

        //find the row which can store 0-column
        for (int i = 0; i < m && posOfFirstZeroRow == -1; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                if (matrix[i][j] == 0)
                {
                    posOfFirstZeroRow = i;
                    break;
                }
            }
        }
        if (posOfFirstZeroRow == -1)
            return;
        for (int i = posOfFirstZeroRow + 1; i < m; ++i)
        {
            bool flag = false;
            for (int j = 0; j < n; ++j)
            {
                if (matrix[i][j] == 0)
                {
                    flag = true;
                    matrix[posOfFirstZeroRow][j] = 0;
                }
            }
            //set the row to 0
            if (flag)
            {
                for (int j = 0; j < n; ++j)
                    matrix[i][j] = 0;
            }
        }

        //set the value of 0-column
        for (int i = 0; i < n; ++i)
        {
            if (matrix[posOfFirstZeroRow][i] == 0)
            {
                for (int j = 0; j < m; ++j)
                    matrix[j][i] = 0;
            }
        }

        for (int i = 0; i < n; ++i)
            matrix[posOfFirstZeroRow][i] = 0;
    }
};
A2A 协议详解:Agent Card 的设计与实现 Agent Card 是 Agent2Agent (A2A) 协议中实现智能体发现与互操作性的核心组件。本文将深入剖析 Agent Card 的设计理念、关键字段及其在智能体生态系统中的作用。我们将通过详细的 JSON Schema 解析和 Python SDK 示例,帮助您理解如何精确地定义和发布智能体能力,为构建可发现、可协作的 AI 智能体奠定基础。当您使用a2a.Agent类初始化一个智能体时,SDK 会根据您提供的参数(nameversion等)自动构建内部的 Agent Card 表示。 阅读详情

相关推荐

OpenClaw(原Clawdbot/Moltbot)本地部署快速指南

支持Telegram、Slack、Discord、iMessage等多平台接入,通过配置文件或环境变量填写对应平台的Token,配置后执行。支持对接OpenAI、Anthropic、阿里云百炼等云端模型,也可搭配Ollama使用本地模型。默认仅允许已配对用户交互,群聊场景建议开启Docker沙箱模式,在配置文件设置。执行后跟随指引确认安全说明,进入QuickStart模式,默认网关端口为。可查看已加载模型,通过绑定的通讯通道发送测试指令,响应正常即部署完成。,限制非主会话操作权限,提升安全性。

wukangjupingbb的博客 8478

[Leetcode 73, medium] Set Matrix Zeroes

Problem: 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 solu

算法的实践 546

微信公众号数据采集实战:历史文章、互动数据与评论详情全解析

本文提供了一套完整的微信公众号数据采集实战方案,详细解析了如何通过接口调用获取公众号的历史文章列表、单篇文章的互动数据以及详细的用户评论。文章从环境准备、核心参数解析到分页逻辑、批量处理和异常处理,提供了可运行的Python代码示例,旨在帮助开发者高效、稳定地构建自动化采集工具,用于竞品分析、内容研究和舆情监控。

u5v6w7的博客 1597

Leetcode OJ 73 Set Matrix Zeroes [Medium]

题目描述: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 解答: public class Solution { public void setZeroes(int[][] matrix) { boolean fr = false,fc = f

大吱的博客 352

Calibre PEX提参实战:如何用hcell和xcell文件精准屏蔽模块(附避坑指南)

本文深入解析Calibre PEX工具中hcell与xcell文件的协同工作机制,提供模块级寄生参数提取方案。通过实战案例和避坑指南,帮助工程师精准屏蔽已验证IP模块,提升仿真效率和数据一致性,适用于65nm以下工艺节点的复杂SoC设计。

weixin_29275257的博客 627

MIL-STD-1553B协议解析与工程实践指南

MIL-STD-1553B作为军用航空电子系统的核心通信协议,采用命令/响应式串行总线架构,通过双冗余设计实现高可靠性数据传输。该协议定义了三类终端设备(总线控制器、远程终端、总线监视器)和六种标准消息格式,典型应用包括飞行控制系统、导航设备等关键航电组件的实时数据交互。深入解析模式代码机制和状态字位域定义,其中传输向量字(10000)和同步命令(00001)等关键指令在分布式系统时间对齐、故障诊断等场景发挥重要作用。工程实践中需特别注意78Ω终端阻抗匹配、4-12μs响应时间窗口等硬性指标,结合变压器耦合

weixin_33431149的博客 435

【Leetcode】73. Set Matrix Zeroes[Medium]

Description 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: A straight forward solution using O(mn) space is probably a bad idea. A simple imp...

Joyer的博客 145

要配置西门子G120XA变频器实现**端子启停**和**Modbus RTU(485)频率给定

如有问题,可通过STARTER软件在线诊断Modbus通讯状态。要配置西门子G120AX变频器实现。

微:13763355074 4847

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

Ubuntu22.04实战:基于VLLM高效部署Qwen3系列模型并构建Dify智能体

本文详细介绍了在Ubuntu22.04系统上使用VLLM高效部署Qwen3系列模型并集成Dify平台的实战指南。从环境配置、Conda管理到VLLM优化部署,再到Dify智能体构建,提供了全面的技术方案和性能调优建议,帮助开发者快速实现AI模型的高效部署与应用。

weixin_28323057的博客 161

MTK Linux DRM分析(八)- KMS drm_crtc.c

CRTC 在 Linux DRM 子系统中是显示控制的核心,负责时序生成、模式设置和管线的调度。理解其工作原理和 API 对于进行底层图形开发、调试显示问题或 simply understanding how your graphics stack works 都至关重要。

iTom1900 1349

【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

YOLOv8改进策略【卷积层】| CVPR-2023 部分卷积 PConv 轻量化卷积,降低内存占用

本文记录的是利用优化的目标检测方法研究。深度可分离卷积可以减少FLOPs,但会导致更高的内存访问,引起延迟并减慢整体计算。部分卷积利用逐点卷积处理通道冗余,减少模型计算量和内存访问量。Run, Don’t Walk: Chasing Higher FLOPS for Faster Neural Networks及其改进的实现代码如下: 四、创新模块 4.1 改进点⭐ 模块改进方法:直接加入(第五节讲解添加步骤)。模块加入如下:模块改进方法:基于的(第五节讲解添加步骤)。第二种改进方法是对中的进行改进,并将

Limiiiing的博客 1016

Leetcode 73. Set Matrix Zeroes (Medium) (cpp)

Leetcode 73. Set Matrix Zeroes (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 1124

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

leetcode 73. Set Matrix Zeroes (medium)

为了使用常数空间大小,将矩阵的第一行、第一列用来记录是否当前行、列需要全部置为0 class Solution { public: void setZeroes(vector<vector<int>> &matrix) { size_t m = matrix.size(); size_t n = matrix[0].size(); bool ro...

Rh's blog 217

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

Leetcode: 73. Set Matrix Zeroes(Week15, Medium)

Leetcode 73 Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.题意:将矩阵中元素为0的对应行、列全部置0. 思路:遍历整个矩阵,用两个set容器存储0元素的行与列,然后扫描完后,再执行置0操作。其中,对置换行,可以赋值长为n且全为0的vector数

linwh8的博客 454

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

LeetCode Top Interview Questions 73. Set Matrix Zeroes (Java版; Medium)

welcome to my blog LeetCode Top Interview Questions 73. Set Matrix Zeroes (Java版; Medium) 题目描述 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:...

littlehaes的博客 183

【leetcode】73.(Medium)Set Matrix Zeroes

解题思路: 首先空间复杂度为O(m*n)的做法是,直接copy一套matrix来记录 空间复杂度为O(m+n)的做法是设置数组row[m]和column[n]来记录每行每列的0的情况 空间复杂度为O(1)的情况就是设置两个boolean变量,首先判定第一行和第一列是否会最终变为0,然后从第一行第一列(最左上角位置为第0行第0列)开始,更新matrix。最后再根据这两个boolean变量来更新mat...

AXIMI的博客 209

LeetCode-73-Set Matrix Zeroes(数组)-Medium

题意理解: 如果矩阵中某个点的值为0,则该点所在的行和列上所有的数均设置为0; 题目分析: 为了降低空间复杂度,需要在当前数组进行设置为0的操作:使用一个list记录所有值为0的位置,然后再对先前的数组进行对应行列设置为0的操作; 解题代码: public class Solution { private class Pair{ publi

eddy_liu的专栏 960

【LeetCode】(73)Set Matrix Zeroes(Medium)

题目 Set Matrix Zeroes  Total Accepted: 43426 Total Submissions: 137908My Submissions Question  Solution  Given a m x n matrix, if an element is 0, set its entire row and column

Row Row Row your boat 311

LeetCode 73. Set Matrix Zeroes(矩阵置零) 数组/medium

文章目录1.Description2.Example3.Solution1.使用标记数组2.使用两个标记变量 1.Description 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。 进阶: 一个直观的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。 你能想出一个仅使用常量空间的解决方案吗? 2.Example 输入:matri

押切徹的博客 252

【LeetCode】解题73:Set Matrix Zeroes

LeetCode解题 73:Set Matrix Zeroes Problem 73: Set Matrix Zeroes [Medium]解题思路Solution (Java) Problem 73: Set Matrix Zeroes [Medium] Given a m x n matrix, if an element is 0, set its entire row and column...

Fayedily的博客 205

[LeetCode][73] Set Matrix Zeroes

题目大意:给定一个矩阵 如果矩阵中出现元素0 则元素所在的行和列都被清空为0 Emmmm 本题算法很简单,所以采用std::multimap 目的是熟悉这个容器 算法当然不是很快。。。毕竟O(n3) /* * @lc app=leetcode id=73 lang=cpp * * [73] Set Matrix Zeroes * * https://leetcode.com/prob...

OCEANtroye的博客 288

Leetcode解题报告:73. 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 solution

wengyunpeng的博客 352

Leetcode刷题记—— 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. click to show follow up. Subscribe to see which companies ask

爱胡椒的小白兔呢 874

LeetCode73 Set Matrix Zeroes

题目: Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.(Medium) 分析: 题意很简单,就是把为0的元素所在的行和列都置0; 首先不能直接边循环边做,这样会造成很多本来不是0的元素被置0之后,在后续判断中将它的行列也置0; 所以简单的方法是建立两...

weixin_30273501的博客 93
上一篇: 预处理指令
下一篇: LeetCode 42. Trapping Rain Water(Hard)
彼得.攀
博客等级 码龄11年 11粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值