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
相关推荐
pride.li4 分钟前
第12章 Python 教程-流程控制:循环结构
linux·服务器·python·算法
鬼手点金22 分钟前
模型即 Agent:当模型本身成为产品
开发语言·前端·人工智能·python·机器学习
田里的水稻28 分钟前
PyTorch-Dataset\DataLoader数据加载
人工智能·pytorch·python
卷无止境34 分钟前
Python 3.14 自由线程:拆掉 GIL 之后,我们到底得到了什么
后端·python
染指111035 分钟前
147.Agent-LangGraph归并函数
开发语言·人工智能·python·langchain·agents
小白学大数据1 小时前
逆向分析短视频平台 a_bogus 参数:从 JavaScript 混淆到 Python 复现
javascript·python·音视频
外收内放1 小时前
数据分析(数据清洗)
python·学习·数据分析
小小张说故事1 小时前
Python 的 True + True 为什么等于 2?bool 是 int 子类,这个设计坑了多少人
python
Nil2082 小时前
leetcode 75颜色分类
数据结构·算法·leetcode
神仙别闹2 小时前
基于Python实现自动化测试作业—图像增广方案
图像处理·python·计算机视觉