【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
相关推荐
特立独行的猫A1 小时前
用仓颉语言 cj-tauri 框架,五分钟给娃做了个鸿蒙形近字游戏
java·后端·python
大可不必了6 小时前
03 函数定义和调用
开发语言·python
I_belong_to_jesus6 小时前
C++关于分支预测的优化1
开发语言·c++·算法
承渊政道7 小时前
【Python编程—从入门到实践】(记分与闯关:用Python+Pygame,打造越战越精彩的太空大战!)
开发语言·python·pycharm·游戏程序·pygame
爱学习的程序媛7 小时前
Python常用框架梳理
开发语言·后端·python·ai·web·后端框架·python框架
imgsq7 小时前
GESP Python 一到四级:备考路线与真题特点
python·青少年编程
eggwyw9 小时前
Anaconda安装卸载的详细流程指南
开发语言·python
2601_967979129 小时前
纯手写:Python安装pip完整教程,搞了几小时,终于成功
python·教程·pip·安装·包管理
Norna9 小时前
缓存文件从没写出来:一个 .json 被当成目录用了
python