【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
相关推荐
2501_9419820515 小时前
复杂消息格式自动化:图片、视频和自定义卡片的消息体构造
开发语言·php
星辰烈龙15 小时前
黑马程序员Java基础8
java·开发语言
Raink老师15 小时前
第 8 章 Python 中的 I/O
python
sin_hielo15 小时前
leetcode 3531
数据结构·算法·leetcode
micro_cloud_fly15 小时前
langchain langgraph历史会话的 json序列化
python·langchain·json
change_topic15 小时前
c语言实现顺序表和链表(利用了c++的引用)
c语言·开发语言·链表
zmzb010315 小时前
C++课后习题训练记录Day48
数据结构·c++·算法
whitelbwwww15 小时前
Pytorch--张量
开发语言·pytorch·python
小毅&Nora15 小时前
【后端】【工具】短信短链接如何做到“永不丢失“?从哈希冲突到百万QPS的可靠性设计
算法·哈希算法
qy-ll15 小时前
Leetcode100题逐题详解
数据结构·python·学习·算法·leetcode