【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
相关推荐
硬件人某某某24 分钟前
python基于卷积神经网络的桥梁裂缝检测系统(django),附可视化界面,源码
python·cnn·django
DARLING Zero two♡28 分钟前
仓颉GC调优参数:垃圾回收的精密控制艺术
开发语言·仓颉
今日说"法"32 分钟前
Rust探秘:所有权转移在函数调用中的表现
开发语言·后端·rust
java1234_小锋1 小时前
PyTorch2 Python深度学习 - 自动微分(Autograd)与梯度优化
开发语言·python·深度学习·pytorch2
Python私教1 小时前
C 语言运算符全景:从入门到进阶
c语言·开发语言·网络
java1234_小锋1 小时前
PyTorch2 Python深度学习 - 简介以及入门
python·深度学习·pytorch2
csbysj20202 小时前
Perl 格式化输出
开发语言
tao3556673 小时前
【Python刷力扣hot100】42. Trapping Rain Water
开发语言·python·leetcode
一只安3 小时前
从零开发AI(不依赖任何模型)
人工智能·python
Miraitowa_cheems3 小时前
LeetCode算法日记 - Day 88: 环绕字符串中唯一的子字符串
java·数据结构·算法·leetcode·深度优先·动态规划