【hot100篇-python刷题记录】【矩阵置零】

R5-矩阵篇

印象题,思路即可:

手动置0

无非就是行和列都置0

使用thex和they将该元素的i和y存储起来,再分别遍历thex,将所有y的位置置0

遍历they,将所有x 置0

python 复制代码
class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        m=len(matrix)
        n=len(matrix[0])
        thex=[]
        they=[]
        for i in range(m):
            for j in range(n):
                if matrix[i][j]==0:
                    thex.append(i)
                    they.append(j)
        #清除
        for i in thex:
            for j in range(n):
                matrix[i][j]=0
        
        for j in they:
            for i in range(m):
                matrix[i][j]=0
相关推荐
databook3 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室3 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三5 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试
用户2519162427118 小时前
Python之语言特点
python
刘立军9 小时前
使用pyHugeGraph查询HugeGraph图数据
python·graphql
聚客AI10 小时前
🙋‍♀️Transformer训练与推理全流程:从输入处理到输出生成
人工智能·算法·llm
数据智能老司机12 小时前
精通 Python 设计模式——创建型设计模式
python·设计模式·架构
大怪v13 小时前
前端:人工智能?我也会啊!来个花活,😎😎😎“自动驾驶”整起!
前端·javascript·算法
数据智能老司机13 小时前
精通 Python 设计模式——SOLID 原则
python·设计模式·架构
c8i14 小时前
django中的FBV 和 CBV
python·django