【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
相关推荐
wuyk5555 分钟前
13.堆排序:基于完全二叉树的高效排序算法一、什么是堆排序?
开发语言·算法·排序算法
runningshark6 分钟前
Lecture: The ‘Why & How‘ Principle: Moving Beyond Simple Statements
开发语言·前端·javascript
qinzechen9 分钟前
本周科技行业热点汇总·2026第36周(2026年8月31日-9月6日)
c++·科技·算法
天衍四九-11 分钟前
Agent Skills从入门到工程化(十六):面试中如何讲清楚 Agent Skills?
大数据·数据库·人工智能·python·chatgpt·面试
2601_9620779817 分钟前
Python中数据可视化的新层次
python·plotly·数据可视化·交互式图表·单行代码
学逆向的22 分钟前
PE——RVA与FOA的转换
开发语言·网络安全·pe
白狐_79824 分钟前
408 数据结构|红黑树 vs AVL:高频考点、易错判断与可能出题方式
数据结构·算法
zz-zjx27 分钟前
Python实用转换模板
开发语言·前端·python
zuozong_30 分钟前
C++类与对象
开发语言·c++·算法
hansang_IR34 分钟前
【代码】分层最短路模板
c++·算法·最短路