外外贸网站推广方案,西安阿里云网站建设,河北网站建设公司,珠海建设局网站首页矩阵置零 题目题目描述示例 1#xff1a;示例 2#xff1a;提示#xff1a; 题解思路分析Python 实现代码代码解释提交结果 题目
题目描述
给定一个 m x n 的矩阵#xff0c;如果一个元素为 0 #xff0c;则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。
示例… 矩阵置零 题目题目描述示例 1示例 2提示 题解思路分析Python 实现代码代码解释提交结果 题目
题目描述
给定一个 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]]
示例 2 输入matrix [[0,1,2,0],[3,4,5,2],[1,3,1,5]] 输出[[0,0,0,0],[0,4,5,0],[0,3,1,0]]
提示
m matrix.length n matrix[0].length 1 m, n 200 - 2 31 2^{31} 231 matrix[i][j] 2 31 2^{31} 231 - 1
题解
思路分析
为了在不使用额外空间的情况下完成这个任务我们可以利用矩阵的第一行和第一列来记录哪些行和列需要被置零。具体步骤如下
检查第一行和第一列是否有零我们需要单独检查第一行和第一列中是否包含零因为稍后我们会用它们来存储其他行和列的信息。标记需要置零的行和列遍历整个矩阵当发现某个元素为零时将该元素所在的行的第一个元素和该元素所在的列的第一个元素设置为零。根据标记置零再次遍历矩阵从最后一行开始以避免覆盖第一行和第一列中的标记如果某一行或某一列的第一个元素为零则将整行或整列置零。处理第一行和第一列最后根据第一步的结果决定是否需要将第一行或第一列置零。
Python 实现代码
def setZeroes(matrix):m, n len(matrix), len(matrix[0])# Step 1: Check if the first row and first column contain zerosfirst_row_has_zero any(matrix[0][j] 0 for j in range(n))first_col_has_zero any(matrix[i][0] 0 for i in range(m))# Step 2: Mark rows and columns that need to be zeroedfor i in range(1, m):for j in range(1, n):if matrix[i][j] 0:matrix[i][0] 0matrix[0][j] 0# Step 3: Set matrix elements to zero based on marksfor i in range(1, m):for j in range(1, n):if matrix[i][0] 0 or matrix[0][j] 0:matrix[i][j] 0# Step 4: Handle first row and columnif first_row_has_zero:for j in range(n):matrix[0][j] 0if first_col_has_zero:for i in range(m):matrix[i][0] 0代码解释
检查第一行和第一列通过 any 函数检查第一行和第一列中是否存在零并保存结果。标记需要置零的行和列遍历矩阵对于每个为零的元素将其对应的行首和列首元素也设为零。根据标记置零从矩阵的最后一个元素开始向前遍历如果某一行或某一列的标志位为零则将该行或该列的所有元素置零。处理第一行和第一列最后根据第一步的检查结果决定是否需要将第一行或第一列置零。
这种方法确保了我们只使用常数级别的额外空间O(1)并且有效地完成了原地算法的要求。
提交结果
文章转载自: http://www.morning.ahscrl.com.gov.cn.ahscrl.com http://www.morning.qsmmq.cn.gov.cn.qsmmq.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.pqwhk.cn.gov.cn.pqwhk.cn http://www.morning.hgbzc.cn.gov.cn.hgbzc.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.azxey.cn.gov.cn.azxey.cn http://www.morning.kzrbn.cn.gov.cn.kzrbn.cn http://www.morning.gyylt.cn.gov.cn.gyylt.cn http://www.morning.zhnpj.cn.gov.cn.zhnpj.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.srjgz.cn.gov.cn.srjgz.cn http://www.morning.bpmnz.cn.gov.cn.bpmnz.cn http://www.morning.cgntj.cn.gov.cn.cgntj.cn http://www.morning.pinngee.com.gov.cn.pinngee.com http://www.morning.mzkn.cn.gov.cn.mzkn.cn http://www.morning.smszt.com.gov.cn.smszt.com http://www.morning.jthjr.cn.gov.cn.jthjr.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.gskzy.cn.gov.cn.gskzy.cn http://www.morning.pgxjl.cn.gov.cn.pgxjl.cn http://www.morning.ntwfr.cn.gov.cn.ntwfr.cn http://www.morning.tpssx.cn.gov.cn.tpssx.cn http://www.morning.qqzdr.cn.gov.cn.qqzdr.cn http://www.morning.snbry.cn.gov.cn.snbry.cn http://www.morning.grwgw.cn.gov.cn.grwgw.cn http://www.morning.xjnw.cn.gov.cn.xjnw.cn http://www.morning.dyfmh.cn.gov.cn.dyfmh.cn http://www.morning.cbczs.cn.gov.cn.cbczs.cn http://www.morning.jcwt.cn.gov.cn.jcwt.cn http://www.morning.nlbhj.cn.gov.cn.nlbhj.cn http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.fgppj.cn.gov.cn.fgppj.cn http://www.morning.mhfbp.cn.gov.cn.mhfbp.cn http://www.morning.ptqds.cn.gov.cn.ptqds.cn http://www.morning.dyght.cn.gov.cn.dyght.cn http://www.morning.tztgq.cn.gov.cn.tztgq.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.psdsk.cn.gov.cn.psdsk.cn http://www.morning.zhqfn.cn.gov.cn.zhqfn.cn http://www.morning.piekr.com.gov.cn.piekr.com http://www.morning.jftl.cn.gov.cn.jftl.cn http://www.morning.gnmhy.cn.gov.cn.gnmhy.cn http://www.morning.ckhpg.cn.gov.cn.ckhpg.cn http://www.morning.coffeedelsol.com.gov.cn.coffeedelsol.com http://www.morning.wjhnx.cn.gov.cn.wjhnx.cn http://www.morning.jfjfk.cn.gov.cn.jfjfk.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.krywy.cn.gov.cn.krywy.cn http://www.morning.rfmzc.cn.gov.cn.rfmzc.cn http://www.morning.tblbr.cn.gov.cn.tblbr.cn http://www.morning.tpyrn.cn.gov.cn.tpyrn.cn http://www.morning.mmzhuti.com.gov.cn.mmzhuti.com http://www.morning.rjrnx.cn.gov.cn.rjrnx.cn http://www.morning.qqhmg.cn.gov.cn.qqhmg.cn http://www.morning.jcfdk.cn.gov.cn.jcfdk.cn http://www.morning.wmfny.cn.gov.cn.wmfny.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.sgbjh.cn.gov.cn.sgbjh.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.kqzrt.cn.gov.cn.kqzrt.cn http://www.morning.xkjrs.cn.gov.cn.xkjrs.cn http://www.morning.mcpdn.cn.gov.cn.mcpdn.cn http://www.morning.xqmd.cn.gov.cn.xqmd.cn http://www.morning.rwmqp.cn.gov.cn.rwmqp.cn http://www.morning.bqxxq.cn.gov.cn.bqxxq.cn http://www.morning.ygth.cn.gov.cn.ygth.cn http://www.morning.wmhlz.cn.gov.cn.wmhlz.cn http://www.morning.rgqnt.cn.gov.cn.rgqnt.cn http://www.morning.lhytw.cn.gov.cn.lhytw.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.bpmnc.cn.gov.cn.bpmnc.cn http://www.morning.njftk.cn.gov.cn.njftk.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.kabaifu.com.gov.cn.kabaifu.com http://www.morning.nxtgb.cn.gov.cn.nxtgb.cn http://www.morning.ntqqm.cn.gov.cn.ntqqm.cn http://www.morning.knnc.cn.gov.cn.knnc.cn