LeetCode|73.矩阵置零

【力扣题解 Day 9】73. 矩阵置零 力扣算法打卡记录 阅读详情

LeetCode习题答案汇总

题目:
给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。

示例 1:

输入:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
输出:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
示例 2:

输入:
[
[0,1,2,0],
[3,4,5,2],
[1,3,1,5]
]
输出:
[
[0,0,0,0],
[0,4,5,0],
[0,3,1,0]
]

分析:

  • 这里使用了额外的空间来记录0所在的行和列
  • 通过遍历扫描记录0元素所在的行和列
  • 再次通过循环检查所标记和行和列,将对应的元素置零

代码:

class Solution:
    def setZeroes(self, matrix: List[List[int]]) -> None:
        """
        Do not return anything, modify matrix in-place instead.
        """
        r = len(matrix)
        c = len(matrix[0])
        rows = set()
        cols = set()
        for i in range(r):
            for j in range(c):
                if matrix[i][j] == 0:
                    rows.add(i)
                    cols.add(j)
        for i in range(r):
            for j in range(c):
                if i in rows or j in cols:
                    matrix[i][j] = 0
                    

结果:
在这里插入图片描述

Leetcode 73:矩阵置零(最详细的解法!!!) 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法**。** 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输出: [ [1,0,1], [0,0,0], [1,0,1] ] 示例 2: 输入: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] 输... 阅读详情

相关推荐

【中等】力扣算法题解析LeetCode73:矩阵置零

本文介绍了一种原地算法,用于将矩阵中零元素所在的行和列全部置零。通过复用矩阵的首行和首列作为标记空间,结合两个额外变量记录首行首列自身的零元素状态,实现了O(1)空间复杂度。算法分为标记阶段和置零阶段:先标记需要置零的行列,再根据标记进行置零操作,最后处理首行首列。这种方法避免了标记覆盖问题,确保了正确性。代码实现展示了详细的处理逻辑,时间复杂度为O(mn),空间复杂度为O(1)。

ylumwd的博客 987

LeetCode73-矩阵置零

LeetCode73-矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输出: [ [1,0,1], [0,0,0], [1,0,1] ] 示例 2: 输入: [ [0,1,2,0], [3,4,5,2], [...

lyd1995的博客 692

三维(点云)语义分割概述以及方法总结

语义分割 语义分割需要两部分 classification: 需要全局信息 segmentation:更加依赖全局信息和局部信息 一般的网络结构是: 提特征-特征映射-特征图压缩(降维)-全连接-分类,其实就是encoder-decoder的过程, 比如在二维的的pspnet,fcn等等 0. 三维表示的数据结构 Point cloud 无序性 点云实际上是无序的,比如有8个点云,你放到...

renhaofan的博客 3万+

LeetCode第73题_矩阵置零

给定一个m x n的矩阵,如果一个元素为,则将其所在行和列的所有元素都设为。请使用算法。

qq_40263592的博客 1496

AI算力网络与通信领域流量分析:优化策略探讨_副本

今天的AI就像一个"超级吃货",每天要"吃"掉海量数据、“消耗"巨量算力——训练一个千亿参数的大模型,可能需要上万块GPU同时工作数周,其间产生的数据传输量足以填满数百万个1TB硬盘。这些GPU和计算节点通过网络连接形成"AI算力网络”,而数据在节点间的流动就是"通信流量"。如果把算力比作AI的"肌肉",那流量就是"血液"——血液不通畅,肌肉再强也使不上劲。

AI天才研究院 1219

隐语第四课:快速上手-SecretFlow的安装和部署

隐语课程,继续学习隐私计算技术

weixin_41467180的博客 2519

73. 矩阵置零 - 力扣(LeetCode)

如果 i 在 row 集合中(即 i == 0 或 i == 2),或者 j 在 col 集合中(即 j == 1 或 j == 2),则将该元素置零。输入:matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]输入:matrix = [[1,1,1],[1,0,1],[1,1,1]][4, 0, 0], # 第 1 行的第 1 和第 2 列被置零。输出:[[0,0,0,0],[0,4,5,0],[0,3,1,0]]输出:[[1,0,1],[0,0,0],[1,0,1]]

q1111111da的博客 537

银河麒麟V10系统下,手把手解决Ollama安装报错‘GLIBCXX_3.4.25 not found’

本文详细介绍了在银河麒麟V10系统下解决Ollama安装报错‘GLIBCXX_3.4.25 not found’的实战指南。通过隔离编译新版libstdc++、环境变量引导和服务化封装等安全方案,确保在不破坏系统稳定性的前提下顺利运行Ollama,适用于国产操作系统与AI工具的兼容性需求。

weixin_42556197的博客 489

LeetCode 力扣 73. 矩阵置零

题目描述(中等难度) 给定一个矩阵,然后找到所有含有 0 的地方,把该位置所在行所在列的元素全部变成 0。 解法一 暴力解法,用一个等大的空间把给定的矩阵存起来,然后遍历这个矩阵,遇到 0 就把原矩阵的当前行,当前列全部变作 0,然后继续遍历。 public void setZeroes(int[][] matrix) { int row = matrix.length; int...

wind_liang的博客 986

《谷歌数据分析方法》脑图笔记

目录壹、脑图笔记贰、《谷歌数据分析方法》脑图文稿[^1]一、 反思数据分析二、选择数据分析工具的最佳策略第一步:选择数据分析工具前要回答的3个关键问题第二步:选择数据分析工具前要问供应商的10个问题第三步:如何有效地试用数据分析工具第四步:确定合作前的协议,检查供应商的服务条款三、点击流分析的精彩世界:指标8个关键网站指标数据分析指标揭秘关键网站指标的战略性策略四、成功的关键:衡量绩效关注“少数关键指标”衡量非电子商务网站的成功衡量B2B网站五、调研和测试永不过时的3个最佳调研问题选择线上调研供应商的8个建

积跬步,慕至千里的博客 2937

主流几个JDK版本:JDK 8、JDK 11、JDK 17 和 JDK 21 的区别

主流几个JDK版本:JDK 8、JDK 11、JDK 17 和 JDK 21 的区别

技术分享 1752

LeetCode 73. 矩阵置零——每日一题

给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0 。请使用 原地 算法。

晴空๓ 2287

目标检测旋转增强源码带方向

解法1: import cv2 import math import numpy as np import os def get_degree(p1,p0): aaa = math.degrees(math.atan2((p1[1] - p0[1]), (p1[0] - p0[0]))) if aaa < 0: aaa = 360 + aaa aaa = (aaa + 90) if aaa > 360: aa...

jacke121的专栏 2089

【LeetCode-73.矩阵置零】

难度:中。

Hermione_pudding的博客 586

OpenHarmony AI系列:神经网络处理单元(NPU)驱动架构与实现详解

NPU(Neural Processing Unit,神经网络处理单元)是专门为神经网络计算设计的硬件加速器。在OpenHarmony系统中,NPU驱动是通过NNRT(Neural Network Runtime,神经网络运行时)框架实现的,它作为中间桥梁连接上层AI推理框架和底层NPU硬件,实现AI模型的跨芯片推理计算。

m0_67505153的博客 669

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Leetcode73. 矩阵置零

则将其所在行和列的所有元素都设为。希望自己可以坚持下去。方法--天涯明月 共勉。的矩阵,如果一个元素为。

GQ_666的博客 1000

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

LeetCode 73 矩阵置零

LeetCode 73 矩阵置零

qq_43745578的博客 983

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

73. 矩阵置零【 力扣(LeetCode) 】

给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0。请使用 原地 算法。

yyh520025的博客 759

LeetCode 73矩阵置零

LeetCode 73矩阵置零 题目简述:给定一个 m x n的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。 示例: 输入: 输出: [ [ [1,1,1], [1,0,1], [1,0,1], [0,0,0], [1,1,1] [1,0,1] ] ] 思路一:利用额外空间存储为0的坐标,根据X Y坐标

XBlog 196

leetcode 73.矩阵置零

为什么不能边遍历边改?如果在遍历到时立刻把第i行全改成 0,那么当你遍历到该行后面的元素时,会误以为它们原本就是 0,从而导致错误的连锁反应(整个矩阵可能全变 0)。方案二的执行顺序为什么这么重要?先检查首行首列:防止标记覆盖原始信息。中间先标记后清洗:利用首行首列记录信息,然后清洗内部数据。最后处理首行首列:如果在清洗内部数据之前就先把首行首列清零了,那么内部数据在判断时就会全部命中,导致错误。面试回答策略“首先,我们可以用两个数组来记录行和列的状态,这样逻辑很清晰,时间复杂度OMN。

2403_86950176的博客 380

力扣(LeetCode)100题:73.矩阵置零 54.螺旋矩阵

本文包含两个矩阵操作算法:矩阵置零和螺旋遍历。矩阵置零算法分两步:首先扫描矩阵记录含0的行列,然后根据记录将对应行列置零。螺旋遍历算法采用按层模拟方法,通过维护四个边界变量(上、下、左、右)依次沿四个方向遍历矩阵元素,每次完成一个方向后收缩对应边界并检查是否越界,确保遍历路径呈现螺旋状。两个算法都采用原地修改和边界控制策略,时间复杂度均为O(mn)。

sweet_ran的博客 404

leetcode73-矩阵置零

leetcode73-矩阵置零

weixin_45799371的博客 668

leetcode73.矩阵置零

第二次循环遍历除了0行0列元素,在置零行|列元素置零,注意先不动0行0列元素;根据第0行|第0列是否有0标记,对第0行元素进行修改。思路:直接上进阶解法思路——仅使用常量空间解决方案。第一次循环遍历,记录置零的行号和列号。,则将其所在行和列的所有元素都设为。(0行 0列用来记录置零行列位置)的矩阵,如果一个元素为。

m0_64995001的博客 277

LeetCode第73题:矩阵置零(中等)

LeetCode第73题:矩阵置零(中等) 题目:给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 解题思路:遍历数组,记录为0的位置坐标。根据坐标依次将行、列变为0。 class Solution { public void setZeroes(int[][] matrix) { //数组的行和列 ...

new_whiter的博客 301
上一篇: LeetCode|541.反转字符串II
下一篇: LeetCode|剑指 Offer 03.数组中重复的数字
xiao黄
xiao黄 优质创作者: 人工智能技术领域 优质创作者: 人工智能技术领域 领域专家: 数据科学与机器学习技术领域 领域专家: 数据科学与机器学习技术领域
博客等级 码龄8年 1万+粉丝 250原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiao黄

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址