【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
相关推荐
曹牧3 小时前
C#:The operation has timed out
开发语言·c#
你看我名字3 小时前
数眼智能 Search + Reader 实战:联网问答怎样避免“搜到了,却引用错了”
android·java·开发语言
重生之小比特3 小时前
【算法拾光】前缀和|连续的子数组和 + 和相同的二元子数组
算法·leetcode·哈希算法
小嘉丶学长3 小时前
Day13 · 列表与元组
linux·运维·服务器·开发语言·python
努力努力再努力wz3 小时前
【Lua入门系列】从 C++ 迭代器到 Lua 迭代器:一文深入理解 Lua 有状态以及无状态迭代器与泛型for、闭包、ipairs、pairs 和 next
开发语言·c++·lua
Pniubi3 小时前
力扣 102二叉树的层序遍历(广度优先搜索)
leetcode·宽度优先
一去不复返的通信er3 小时前
力扣15:盛最多水的容器
算法·leetcode
小小张说故事3 小时前
Python 的 int 为什么不会溢出?拆开 PyLongObject 和它背后那串变长数组
python
努力实干3 小时前
第二阶段 · 第 7 课:C++ 多线程与线程安全
开发语言·c++·安全
Navigator_Z3 小时前
LeetCode //C - 1309. Decrypt String from Alphabet to Integer Mapping
c语言·算法·leetcode