Python | Leetcode Python题解之第73题矩阵置零

题目:

题解:

python 复制代码
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        m, n = len(matrix), len(matrix[0])
        flag_col0 = False
        
        for i in range(m):
            if matrix[i][0] == 0:
                flag_col0 = True
            for j in range(1, n):
                if matrix[i][j] == 0:
                    matrix[i][0] = matrix[0][j] = 0
        
        for i in range(m - 1, -1, -1):
            for j in range(1, n):
                if matrix[i][0] == 0 or matrix[0][j] == 0:
                    matrix[i][j] = 0
            if flag_col0:
                matrix[i][0] = 0
相关推荐
APIshop14 分钟前
代码实例:Python 爬虫抓取与解析 JSON 数据
爬虫·python·json
程序员爱钓鱼26 分钟前
Python编程实战 · 基础入门篇 | Python的版本与安装
后端·python
hmbbcsm37 分钟前
练习python题目小记
python
ZhengEnCi1 小时前
CMD 与 Python 完全区别指南-小白必看的编程入门解惑宝典
windows·python
Hs_QY_FX1 小时前
逻辑回归实战:泰坦尼克号生存预测
python·逻辑回归
惜月_treasure2 小时前
LlamaIndex多模态RAG开发实现详解
开发语言·python·机器学习
吗~喽2 小时前
【LeetCode】四数之和
算法·leetcode·职场和发展
扶尔魔ocy2 小时前
python 部署可离线使用的中文识别OCR(window)
python·中文识别·cnocr
一晌小贪欢3 小时前
Python爬虫第4课:XPath与lxml高级解析技术
开发语言·爬虫·python·网络爬虫·python爬虫·python3·python办公
虚行3 小时前
C#项目连接S7-PLCSIM Advanced读写操作
开发语言·python·c#