矩阵的置—-LeetCode Hot100(14/100)——73. 矩阵置零 - node
文章目录
一、题目概述示例
二、思维导图三、解法一:暴力法(不推荐)原理说明改进思路实现步骤时间与空间复杂度
四、解法二:使用辅助数组标记行列原理说明步骤流程时间与空间复杂度Java代码实现
五、解法三:原地标记(最优方法)核心思想步骤解析算法流程图时间与空间复杂度Java代码实现
六、各方法性能对比表七、总结
一、题目概述
给定一个 m x n 的二维矩阵,如果某个元素为 0,则将其所在行与列的所有元素都设为 0。你需要 原地修改 输入矩阵(即不使用额外的矩阵存储结果)。
示例
输入:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
输出:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
说明:第 2 行第 2 列的元素为 0,因此第 2 行和第 2 列都需要置零。
二、思维导图
下面的思维导图展示了问题的整体思路,从暴力法到最优解的演进路线:
#mermaid-svg-cec0BaunMp2dj4Pl{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-cec0BaunMp2dj4Pl .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-cec0BaunMp2dj4Pl .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-cec0BaunMp2dj4Pl .error-icon{fill:#552222;}#mermaid-svg-cec0BaunMp2dj4Pl .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-cec0BaunMp2dj4Pl .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-cec0BaunMp2dj4Pl .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-cec0BaunMp2dj4Pl .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-cec0BaunMp2dj4Pl .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-cec0BaunMp2dj4Pl .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-cec0BaunMp2dj4Pl .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-cec0BaunMp2dj4Pl .marker{fill:#333333;stroke:#333333;}#mermaid-svg-cec0BaunMp2dj4Pl .marker.cross{stroke:#333333;}#mermaid-svg-cec0BaunMp2dj4Pl svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-cec0BaunMp2dj4Pl p{margin:0;}#mermaid-svg-cec0BaunMp2dj4Pl .edge{stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .section--1 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section--1 path,#mermaid-svg-cec0BaunMp2dj4Pl .section--1 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section--1 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section--1 path{fill:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section--1 text{fill:#ffffff;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon--1{font-size:40px;color:#ffffff;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge--1{stroke:hsl(240, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth--1{stroke-width:17;}#mermaid-svg-cec0BaunMp2dj4Pl .section--1 line{stroke:hsl(60, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-0 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-0 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-0 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-0 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-0 path{fill:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-0 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-0{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-0{stroke:hsl(60, 100%, 73.5294117647%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-0{stroke-width:14;}#mermaid-svg-cec0BaunMp2dj4Pl .section-0 line{stroke:hsl(240, 100%, 83.5294117647%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-1 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-1 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-1 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-1 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-1 path{fill:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-1 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-1{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-1{stroke:hsl(80, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-1{stroke-width:11;}#mermaid-svg-cec0BaunMp2dj4Pl .section-1 line{stroke:hsl(260, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-2 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-2 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-2 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-2 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-2 path{fill:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-2 text{fill:#ffffff;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-2{font-size:40px;color:#ffffff;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-2{stroke:hsl(270, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-2{stroke-width:8;}#mermaid-svg-cec0BaunMp2dj4Pl .section-2 line{stroke:hsl(90, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-3 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-3 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-3 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-3 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-3 path{fill:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-3 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-3{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-3{stroke:hsl(300, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-3{stroke-width:5;}#mermaid-svg-cec0BaunMp2dj4Pl .section-3 line{stroke:hsl(120, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-4 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-4 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-4 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-4 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-4 path{fill:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-4 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-4{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-4{stroke:hsl(330, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-4{stroke-width:2;}#mermaid-svg-cec0BaunMp2dj4Pl .section-4 line{stroke:hsl(150, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-5 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-5 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-5 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-5 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-5 path{fill:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-5 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-5{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-5{stroke:hsl(0, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-5{stroke-width:-1;}#mermaid-svg-cec0BaunMp2dj4Pl .section-5 line{stroke:hsl(180, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-6 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-6 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-6 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-6 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-6 path{fill:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-6 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-6{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-6{stroke:hsl(30, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-6{stroke-width:-4;}#mermaid-svg-cec0BaunMp2dj4Pl .section-6 line{stroke:hsl(210, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-7 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-7 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-7 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-7 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-7 path{fill:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-7 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-7{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-7{stroke:hsl(90, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-7{stroke-width:-7;}#mermaid-svg-cec0BaunMp2dj4Pl .section-7 line{stroke:hsl(270, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-8 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-8 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-8 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-8 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-8 path{fill:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-8 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-8{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-8{stroke:hsl(150, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-8{stroke-width:-10;}#mermaid-svg-cec0BaunMp2dj4Pl .section-8 line{stroke:hsl(330, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-9 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-9 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-9 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-9 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-9 path{fill:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-9 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-9{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-9{stroke:hsl(180, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-9{stroke-width:-13;}#mermaid-svg-cec0BaunMp2dj4Pl .section-9 line{stroke:hsl(0, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-10 rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-10 path,#mermaid-svg-cec0BaunMp2dj4Pl .section-10 circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-10 polygon,#mermaid-svg-cec0BaunMp2dj4Pl .section-10 path{fill:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-10 text{fill:black;}#mermaid-svg-cec0BaunMp2dj4Pl .node-icon-10{font-size:40px;color:black;}#mermaid-svg-cec0BaunMp2dj4Pl .section-edge-10{stroke:hsl(210, 100%, 76.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .edge-depth-10{stroke-width:-16;}#mermaid-svg-cec0BaunMp2dj4Pl .section-10 line{stroke:hsl(30, 100%, 86.2745098039%);stroke-width:3;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled,#mermaid-svg-cec0BaunMp2dj4Pl .disabled circle,#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:lightgray;}#mermaid-svg-cec0BaunMp2dj4Pl .disabled text{fill:#efefef;}#mermaid-svg-cec0BaunMp2dj4Pl .section-root rect,#mermaid-svg-cec0BaunMp2dj4Pl .section-root path,#mermaid-svg-cec0BaunMp2dj4Pl .section-root circle,#mermaid-svg-cec0BaunMp2dj4Pl .section-root polygon{fill:hsl(240, 100%, 46.2745098039%);}#mermaid-svg-cec0BaunMp2dj4Pl .section-root text{fill:#ffffff;}#mermaid-svg-cec0BaunMp2dj4Pl .section-root span{color:#ffffff;}#mermaid-svg-cec0BaunMp2dj4Pl .section-2 span{color:#ffffff;}#mermaid-svg-cec0BaunMp2dj4Pl .icon-container{height:100%;display:flex;justify-content:center;align-items:center;}#mermaid-svg-cec0BaunMp2dj4Pl .edge{fill:none;}#mermaid-svg-cec0BaunMp2dj4Pl .mindmap-node-label{dy:1em;alignment-baseline:middle;text-anchor:middle;dominant-baseline:middle;text-align:center;}#mermaid-svg-cec0BaunMp2dj4Pl :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
Set Matrix Zeroes
子问题分析
如果某元素为0 → 整行整列置0
解法演进
暴力解法
使用辅助数组
使用标记集合
原地标记法(最优)
复杂度
m*n
空间复杂度: 不同方法从 O(m+n) 降到 O(1)
三、解法一:暴力法(不推荐)
原理说明
当我们发现某个元素为 0 时,立即将其所在行和列全部设为 0。但是这样会有一个问题 —— 更改后的 0 会影响后续判断。
改进思路
考虑用另一个矩阵来保存哪些位置需要被置 0,最后再统一修改。
实现步骤
遍历输入矩阵,记录所有为 0 的位置 (i, j);再次遍历矩阵,将这些位置对应的行列全部置零。
时间与空间复杂度
时间复杂度:O(m * n * (m + n))空间复杂度:O(1)(若直接修改),但逻辑会错误;若使用记录列表,约为 O(m * n)
四、解法二:使用辅助数组标记行列
原理说明
可以利用两个数组:
row[m]:标记每一行是否包含零;col[n]:标记每一列是否包含零。
步骤流程
#mermaid-svg-IL3xpY3wKeCQNEZW{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-IL3xpY3wKeCQNEZW .error-icon{fill:#552222;}#mermaid-svg-IL3xpY3wKeCQNEZW .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-IL3xpY3wKeCQNEZW .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-IL3xpY3wKeCQNEZW .marker{fill:#333333;stroke:#333333;}#mermaid-svg-IL3xpY3wKeCQNEZW .marker.cross{stroke:#333333;}#mermaid-svg-IL3xpY3wKeCQNEZW svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-IL3xpY3wKeCQNEZW p{margin:0;}#mermaid-svg-IL3xpY3wKeCQNEZW .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-IL3xpY3wKeCQNEZW .cluster-label text{fill:#333;}#mermaid-svg-IL3xpY3wKeCQNEZW .cluster-label span{color:#333;}#mermaid-svg-IL3xpY3wKeCQNEZW .cluster-label span p{background-color:transparent;}#mermaid-svg-IL3xpY3wKeCQNEZW .label text,#mermaid-svg-IL3xpY3wKeCQNEZW span{fill:#333;color:#333;}#mermaid-svg-IL3xpY3wKeCQNEZW .node rect,#mermaid-svg-IL3xpY3wKeCQNEZW .node circle,#mermaid-svg-IL3xpY3wKeCQNEZW .node ellipse,#mermaid-svg-IL3xpY3wKeCQNEZW .node polygon,#mermaid-svg-IL3xpY3wKeCQNEZW .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-IL3xpY3wKeCQNEZW .rough-node .label text,#mermaid-svg-IL3xpY3wKeCQNEZW .node .label text,#mermaid-svg-IL3xpY3wKeCQNEZW .image-shape .label,#mermaid-svg-IL3xpY3wKeCQNEZW .icon-shape .label{text-anchor:middle;}#mermaid-svg-IL3xpY3wKeCQNEZW .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-IL3xpY3wKeCQNEZW .rough-node .label,#mermaid-svg-IL3xpY3wKeCQNEZW .node .label,#mermaid-svg-IL3xpY3wKeCQNEZW .image-shape .label,#mermaid-svg-IL3xpY3wKeCQNEZW .icon-shape .label{text-align:center;}#mermaid-svg-IL3xpY3wKeCQNEZW .node.clickable{cursor:pointer;}#mermaid-svg-IL3xpY3wKeCQNEZW .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-IL3xpY3wKeCQNEZW .arrowheadPath{fill:#333333;}#mermaid-svg-IL3xpY3wKeCQNEZW .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-IL3xpY3wKeCQNEZW .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-IL3xpY3wKeCQNEZW .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IL3xpY3wKeCQNEZW .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-IL3xpY3wKeCQNEZW .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IL3xpY3wKeCQNEZW .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-IL3xpY3wKeCQNEZW .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-IL3xpY3wKeCQNEZW .cluster text{fill:#333;}#mermaid-svg-IL3xpY3wKeCQNEZW .cluster span{color:#333;}#mermaid-svg-IL3xpY3wKeCQNEZW div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-IL3xpY3wKeCQNEZW .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-IL3xpY3wKeCQNEZW rect.text{fill:none;stroke-width:0;}#mermaid-svg-IL3xpY3wKeCQNEZW .icon-shape,#mermaid-svg-IL3xpY3wKeCQNEZW .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-IL3xpY3wKeCQNEZW .icon-shape p,#mermaid-svg-IL3xpY3wKeCQNEZW .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-IL3xpY3wKeCQNEZW .icon-shape rect,#mermaid-svg-IL3xpY3wKeCQNEZW .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-IL3xpY3wKeCQNEZW .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-IL3xpY3wKeCQNEZW .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-IL3xpY3wKeCQNEZW :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
是
否
是
否
开始
初始化 row[], col[]
第一次遍历矩阵
元素为0?
标记行与列
继续遍历
第二次遍历矩阵
行或列标记为真?
设0
保持原值
结束
时间与空间复杂度
时间复杂度:O(m * n)空间复杂度:O(m + n)
Java代码实现
public void setZeroes(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
boolean[] row = new boolean[m];
boolean[] col = new boolean[n];
// 记录哪些行和列需要置0
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
row[i] = true;
col[j] = true;
}
}
}
// 根据标记置0
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (row[i] || col[j]) {
matrix[i][j] = 0;
}
}
}
}
五、解法三:原地标记(最优方法)
核心思想
不使用额外空间,而直接利用矩阵的 第一行与第一列 作为标记数组的替代。
步骤解析
首先记录第一行和第一列是否本身含 0;从第二行第二列开始,如果某个元素为 0,就在所在的第一行和第一列位置标记为 0;再次遍历矩阵,当某行或某列的标记位为 0 时,就将该元素置零;最后根据第一行和第一列的记录情况处理它们。
算法流程图
#mermaid-svg-ivGq2FiSwR9bbzDa{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-ivGq2FiSwR9bbzDa .error-icon{fill:#552222;}#mermaid-svg-ivGq2FiSwR9bbzDa .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ivGq2FiSwR9bbzDa .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ivGq2FiSwR9bbzDa .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ivGq2FiSwR9bbzDa .marker.cross{stroke:#333333;}#mermaid-svg-ivGq2FiSwR9bbzDa svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ivGq2FiSwR9bbzDa p{margin:0;}#mermaid-svg-ivGq2FiSwR9bbzDa .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ivGq2FiSwR9bbzDa .cluster-label text{fill:#333;}#mermaid-svg-ivGq2FiSwR9bbzDa .cluster-label span{color:#333;}#mermaid-svg-ivGq2FiSwR9bbzDa .cluster-label span p{background-color:transparent;}#mermaid-svg-ivGq2FiSwR9bbzDa .label text,#mermaid-svg-ivGq2FiSwR9bbzDa span{fill:#333;color:#333;}#mermaid-svg-ivGq2FiSwR9bbzDa .node rect,#mermaid-svg-ivGq2FiSwR9bbzDa .node circle,#mermaid-svg-ivGq2FiSwR9bbzDa .node ellipse,#mermaid-svg-ivGq2FiSwR9bbzDa .node polygon,#mermaid-svg-ivGq2FiSwR9bbzDa .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ivGq2FiSwR9bbzDa .rough-node .label text,#mermaid-svg-ivGq2FiSwR9bbzDa .node .label text,#mermaid-svg-ivGq2FiSwR9bbzDa .image-shape .label,#mermaid-svg-ivGq2FiSwR9bbzDa .icon-shape .label{text-anchor:middle;}#mermaid-svg-ivGq2FiSwR9bbzDa .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-ivGq2FiSwR9bbzDa .rough-node .label,#mermaid-svg-ivGq2FiSwR9bbzDa .node .label,#mermaid-svg-ivGq2FiSwR9bbzDa .image-shape .label,#mermaid-svg-ivGq2FiSwR9bbzDa .icon-shape .label{text-align:center;}#mermaid-svg-ivGq2FiSwR9bbzDa .node.clickable{cursor:pointer;}#mermaid-svg-ivGq2FiSwR9bbzDa .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-ivGq2FiSwR9bbzDa .arrowheadPath{fill:#333333;}#mermaid-svg-ivGq2FiSwR9bbzDa .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ivGq2FiSwR9bbzDa .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ivGq2FiSwR9bbzDa .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ivGq2FiSwR9bbzDa .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-ivGq2FiSwR9bbzDa .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ivGq2FiSwR9bbzDa .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-ivGq2FiSwR9bbzDa .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ivGq2FiSwR9bbzDa .cluster text{fill:#333;}#mermaid-svg-ivGq2FiSwR9bbzDa .cluster span{color:#333;}#mermaid-svg-ivGq2FiSwR9bbzDa div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ivGq2FiSwR9bbzDa .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-ivGq2FiSwR9bbzDa rect.text{fill:none;stroke-width:0;}#mermaid-svg-ivGq2FiSwR9bbzDa .icon-shape,#mermaid-svg-ivGq2FiSwR9bbzDa .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-ivGq2FiSwR9bbzDa .icon-shape p,#mermaid-svg-ivGq2FiSwR9bbzDa .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-ivGq2FiSwR9bbzDa .icon-shape rect,#mermaid-svg-ivGq2FiSwR9bbzDa .image-shape rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-ivGq2FiSwR9bbzDa .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-ivGq2FiSwR9bbzDa .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-ivGq2FiSwR9bbzDa :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}
是
否
初始化
记录第一行是否有0
记录第一列是否有0
遍历剩余元素
元素为0?
标记第一行与第一列
继续遍历
根据标记置0
处理第一行列
结束
时间与空间复杂度
时间复杂度:O(m * n)空间复杂度:O(1) 拼多多帮助力入口
Java代码实现
public void setZeroes(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
boolean firstRowZero = false, firstColZero = false;
// 检查第一行
for (int j = 0; j < n; j++) {
if (matrix[0][j] == 0) {
firstRowZero = true;
break;
}
}
// 检查第一列
for (int i = 0; i < m; i++) {
if (matrix[i][0] == 0) {
firstColZero = true;
break;
}
}
// 使用第一行和第一列标记零
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
// 根据标记置零
for (int i = 1; i < m; i++) {
for (int j = 1; j < n; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
// 处理第一行
if (firstRowZero) {
for (int j = 0; j < n; j++) matrix[0][j] = 0;
}
// 处理第一列
if (firstColZero) {
for (int i = 0; i < m; i++) matrix[i][0] = 0;
}
}
六、各方法性能对比表
方法时间复杂度空间复杂度是否原地修改暴力法O(mn(m+n))高否辅助数组O(m*n)O(m+n)否原地标记O(m*n)O(1)是
七、总结
暴力法容易想到,但效率低;辅助数组平衡了性能与可读性,是较常见的做法;原地标记法是最优解:在不增加额外空间的条件下,完成高效的矩阵置零。
这道题体现了从“额外空间辅助”到“原地替代标记”的优化思维,也是很多二维数组操作题目的共性思路。,矩阵的置