【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
相关推荐
酉鬼女又兒1 分钟前
零基础快速入门前端Web存储(sessionStorage & localStorage)知识点详解与蓝桥杯考点应用(可用于备赛蓝桥杯Web应用开发)
开发语言·前端·javascript·职场和发展·蓝桥杯·html
Bert.Cai2 分钟前
Python集合简介
开发语言·python
Yzzz-F3 分钟前
2018-2019 ACM-ICPC, Asia Dhaka Regional ContestC[数论]
算法
tryCbest6 分钟前
Java和Python开发项目部署简介
java·开发语言·python
ZTLJQ7 分钟前
任务调度的艺术:Python分布式任务系统完全解析
开发语言·分布式·python
阿里嘎多学长8 分钟前
2026-03-31 GitHub 热点项目精选
开发语言·程序员·github·代码托管
Frostnova丶9 分钟前
LeetCode 3474. 字典序最小的生成字符串
算法·leetcode·职场和发展
REDcker9 分钟前
Nagle 算法与 TCP_NODELAY、TCP_CORK 详解
网络·tcp/ip·算法
AlenTech10 分钟前
136. 只出现一次的数字 - 力扣(LeetCode)
leetcode
敏编程13 分钟前
一天一个Python库:isodate - 处理 ISO 8601 日期时间格式
python