【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
相关推荐
写代码的【黑咖啡】6 分钟前
Python 中的 Requests 库:轻松进行 HTTP 请求
开发语言·python·http
BD_Marathon6 分钟前
MyBatis各种查询功能
java·开发语言·mybatis
栗子叶6 分钟前
Spring 中 Servlet 容器和 Python FastAPI 对比
python·spring·servlet·fastapi
研☆香8 分钟前
JavaScript 特点介绍
开发语言·javascript·ecmascript
Howrun77710 分钟前
虚幻引擎_AController_APlayerController_AAIController
开发语言·c++·游戏引擎·虚幻
杨杨杨大侠11 分钟前
DeepAgents 框架深度解析:从理论到实践的智能代理架构
后端·python·llm
曹牧17 分钟前
C#:ToDouble
开发语言·c#
Xの哲學19 分钟前
Linux 软中断深度剖析: 从设计思想到实战调试
linux·网络·算法·架构·边缘计算
袁袁袁袁满20 分钟前
Python读取doc文件打印内容
开发语言·python·python读取doc文件
暴风游侠23 分钟前
如何进行科学的分类
笔记·算法·分类