【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
相关推荐
云和数据.ChenGuang8 分钟前
LangGraph 异步 Node 的设计与写法实践
开发语言·人工智能·python·机器学习·php
天诰1 小时前
SA-K-11 检索会消失吗与四个未解问题
java·服务器·人工智能·python·机器学习·ai编程
benchmark_cc2 小时前
用 QuantDash 读取美股行情:AAPL.US 的运行前检查、排错顺序与数据校验清单
python·数据分析·pandas·量化交易·股票数据·quantdash
蜗牛一步步往上爬3 小时前
第十六章 C++语言在深度学习中的应用
开发语言·c++·深度学习
羲云网络5 小时前
GPT Image 2.6 接入指南:Flare、Sunburst 怎么选,编辑失败如何排查
人工智能·python·aigc·图像生成·acedatacloud
张张123y5 小时前
DSH 插件调试避坑:client.js 设置面板、热重载与高频报错排查表
开发语言·javascript·人工智能·开源·ecmascript·后端开发·ai agent
文丑颜不良啊6 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z7 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
D e re k ZH7 小时前
C++ 标准库算法全景指南
数据结构·c++·算法