【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
相关推荐
xphjj5 分钟前
树形数据模糊搜索
前端·javascript·算法
Sheeep19 分钟前
学习Pytest + Hypothesis——能帮你发现你自己都没想到的 bug
python·测试
Once_day27 分钟前
代码训练LeetCode(24)数组乘积
算法·leetcode
come1123439 分钟前
Claude 写 PHP 项目的完整小白教程
开发语言·php
虾球xz43 分钟前
CppCon 2015 学习:Concurrency TS Editor’s Report
开发语言·c++·学习
板鸭〈小号〉1 小时前
命名管道实现本地通信
开发语言·c++
站大爷IP1 小时前
用Python打造办公效率神器:从数据到文档的全流程自动化实践
python
hongjianMa1 小时前
ModuleNotFoundError No module named ‘torch_geometric‘未找到
python
火兮明兮2 小时前
Python训练第四十五天
开发语言·python
zdy12635746882 小时前
python43天
python·深度学习·机器学习