【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
相关推荐
进击的明明12 小时前
当AI开始“边想边干”:深入聊聊ReAct,以及如何用Python手搓一个简单Agent
python·面试·agent
全栈弄潮儿13 小时前
Python实战第8期:函数
后端·python·编程语言
zyl76515 小时前
FlashAttention 详解:从 GPU 硬件、普通 Attention 到分块计算与 Online Softmax
算法
卷无止境15 小时前
暗流与破局:AI时代的代码安全审查工具全景指南
后端·python
卷无止境17 小时前
一人公司的全栈选型指南:让 AI Agent 写得快,也让生意跑得稳
后端·python
爱读书的小胖19 小时前
LeetCode 122:买卖股票的最佳时机 II(C)解题分析
算法
结局无敌19 小时前
React Native for OpenHarmony 实战:三方库 react-native-modal-dialog 的鸿蒙化适配指南
深度学习·算法·react native·react.js·harmonyos
WJ.Polar19 小时前
Java封装、继承、多态
java·开发语言
摇滚侠19 小时前
Java 中如何表示负数,有符号和无符号的区别,补码的计算规则,char byte short int long 的范围
java·开发语言
滴啦嘟啦哒19 小时前
【具身笔记】【π0.5 微调与仿真】五、在MuJoCo中实现可视化仿真
笔记·python·vla·具身