【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
相关推荐
闲猫4 小时前
LangChain / Core components / Models
开发语言·python·langchain
想做小南娘,发现自己是女生喵4 小时前
第 2 章 顺序表和 vector
java·数据结构·算法
艾醒5 小时前
2026年第29周(7.13-7.19)AI全复盘:技术突破、行业趣闻翻车、算力服务器商业动态
人工智能·算法
雪碧聊技术5 小时前
动态规划算法—01背包问题
算法·动态规划
-银雾鸢尾-5 小时前
C#中的索引器
开发语言·c#
bu_shuo5 小时前
c与cpp中的argc和argv
c语言·c++·算法
普贤莲花5 小时前
【2026年第29周---写于20260718】---整理,断舍离
程序人生·算法·生活
Qlittleboy6 小时前
PHP的接口参数传递的过多,max_input_vars = 5000
开发语言·php
Reart6 小时前
Leetcode 674.最长连续递增序列 (719)
后端·算法
Aurorar0rua6 小时前
CS50 x 2024 Notes Algorithms - 02
c语言·开发语言·学习方法