73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0
[抄题]:
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
Example 1:
Input:
[
[1,1,1],
[1,0,1],
[1,1,1]
]
Output:
[
[1,0,1],
[0,0,0],
[1,0,1]
]
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
[思维问题]:
以为可以直接改,那就全都变成0了啊
[英文数据结构或算法,为什么不用别的数据结构或算法]:
for循环特别多的时候,每次int都需要重新说明
[一句话思路]:
做标记 标记大法好,最后一起改
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
== true 不用写啊亲 专业一点
[二刷]:
if matrix[i][j] == 0 的条件提出来,后面一起写,分开写反而有bug
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
做标记 标记大法好,最后一起改
[复杂度]:Time complexity: O(mn) Space complexity: O(1)
[算法思想:迭代/递归/分治/贪心]:
[关键模板化代码]:
row是0时,处理的是j, 想象一下就行了
//set the edge for x
if (rowSign) {
for (int j = 0; j < col; j++)
matrix[0][j] = 0;
}
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
[潜台词] :
class Solution {
public void setZeroes(int[][] matrix) {
//ini: rowSign, colSign
boolean rowSign = false; boolean colSign = false;
int row = matrix.length; int col = matrix[0].length;
//cc
if(matrix == null || row == 0 || col == 0) return ;
//for loop and make sign
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if(matrix[i][j] == 0) {
if(i == 0) rowSign = true;
if(j == 0) colSign = true;
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}
//set the inside for x
for (int i = 1; i < row; i++) {
if (matrix[i][0] == 0) {
for (int j = 1; j < col; j++) {
matrix[i][j] = 0;
}
}
}
//set the inside for y
for (int j = 1; j < col; j++) {
if (matrix[0][j] == 0) {
for (int i = 1; i < row; i++) {
matrix[i][j] = 0;
}
}
}
//set the edge for x
if (rowSign) {
for (int i = 0; i < row; i++)
matrix[i][0] = 0;
}
//set the edge for y
if (colSign) {
for (int j = 0; j < col; j++)
matrix[0][j] = 0;
}
}
}
73. Set Matrix Zeroes 把矩阵同一行列的元素都改成0的更多相关文章
- leetcode[73] Set Matrix Zeroes 将矩阵置零
给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...
- 73. Set Matrix Zeroes
题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Fo ...
- LeetCode 73. Set Matrix Zeros(矩阵赋零)
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...
- [LeetCode] 73. Set Matrix Zeroes 矩阵赋零
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place. Exampl ...
- LeetCode OJ 73. Set Matrix Zeroes
Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. click ...
- 力扣—set matrix zeroes (矩阵置零) python实现
题目描述: 中文: 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 英文: Given a m x n matrix, if an eleme ...
- LeetCode第[73]题(Java):Set Matrix Zeroes(矩阵置0)
题目:矩阵置0 难度:Easy 题目内容: Given a m x n matrix, if an element is 0, set its entire row and column to 0. ...
- 【LeetCode】73. Set Matrix Zeroes (2 solutions)
Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...
- 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...
随机推荐
- java Scanner中next和nextLine()区别
next(): 1.一定要读取到有效字符后才可以结束输入. 2.对输入有效字符之前遇到的空白,next() 方法会自动将其去掉. 3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符. ne ...
- MongoDB的启动与停止(一)
1:启动和停止Mongodb 1)从命令行启动 执行mongod,启动MongoDB服务器,mongod有很多可配置的启动选项,可以使用mongod --help查看所有选项 -- ...
- delphi XML简单处理
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- EF LIKE 查询
<!-- CSDL content --> <edmx:ConceptualModels> <Schema Namespace="Model" Ali ...
- 由override 和 overload 引发的学习感悟
工作已三年的我,竟然面试的时候去裸考了.想当然的认为有很多东西会在工作中不知不觉积累下来,现在想想,真是扯淡... 我的三年的工作经验是开发测试的工作,主要负责测试用例的自动化实现,稍深一些的是自动化 ...
- 【C++】boost::shared_ptr boost::make_shared
一.shared_ptr shared_ptr作为一个动态分配的对象,当最后一个指向其内容的指针销毁(destroyed)或重置(reset),其指向的内容会被销毁(deleted).不再需要显式调用 ...
- 基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络
基础 在参考①中我们详细介绍了没有隐含层的神经网络结构,该神经网络只有输入层和输出层,并且输入层和输出层是通过全连接方式进行连接的.具体结构如下: 我们用此网络结构基于MNIST数据集(参考②)进行训 ...
- web.xml的加载过程配置详解
一:web.xml加载过程 简单说一下,web.xml的加载过程.当我们启动一个WEB项目容器时,容器包括(JBoss,Tomcat等).首先会去读取web.xml配置文件里的配置,当这一步骤没有 ...
- 44个Java代码性能优化总结
https://blog.csdn.net/xiang__liu/article/details/79321639 ---稍后有时间整理
- leetcode238
public class Solution { public int[] ProductExceptSelf(int[] nums) { int[] result = new int[nums.Len ...