【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
相关推荐
CoderYanger7 小时前
Java SE——12.异常(≠错误)《干货笔记》
java·开发语言
Data_agent7 小时前
1688获得1688店铺所有商品API,python请求示例
java·开发语言·python
一晌小贪欢7 小时前
【Python办公】-图片批量添加文字水印(附代码)
开发语言·python·图片水印·python水印·python添加水印·图片添加水印
why1517 小时前
面经整理——算法
java·数据结构·算法
Yeats_Liao7 小时前
CANN Samples(十三):Ascend C 算子开发入门
c语言·开发语言
越来越无动于衷7 小时前
Java 实现 WebService(SOAP)联网调用:从原理到实战
java·开发语言
海上飞猪7 小时前
【python】基础数据类型
python
悦悦子a啊7 小时前
将学生管理系统改造为C/S模式 - 开发过程报告
java·开发语言·算法
痕忆丶7 小时前
双线性插值缩放算法详解
算法
万邦科技Lafite8 小时前
一键获取淘宝关键词商品信息指南
开发语言·数据库·python·商品信息·开放api·电商开放平台