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 ...
随机推荐
- 第三章泛型集合ArrayList 和Hashtable
第三章泛型集集合 ArrayList 变量名 = new ArrayList(); //相当与一个容器 他的执行using 是 using System.Collections; 变量名.ADD( ...
- .htaccess FollowSymlinks影响rewrite功能
Thinkphp的框架的根目录的.htaccess是这样写的: <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine ...
- qsort函数排序各种类型的数据。
qsort函数是库函数中的一员,我们先来看看官方文档是怎么写的: 其中qsort的参数void* base是传入一个数组,size_t num 为数组整体大小,size_t size 为单个元素的大小 ...
- rsync的daemon模式
官方文档:https://download.samba.org/pub/rsync/rsyncd.conf.html 1:daemon模式配置文件 rsync以daemon方式运行 ...
- zabbix之 自定义(指定特定磁盘)监控io
引言 zabbix自带的模板,并且完成了我们的一些比较常用的监控,现在我们如果想要监控我们磁盘的IO,这时候zabbix并没有给我们提供这么一个模板,所以我们需要自己来创建一个模板来完成磁盘IO的监控 ...
- 简述Ajax原理及实现步骤
简述Ajax原理及实现步骤 1.Ajax简介 概念 Ajax 即“Asynchronous Javascript And XML”(异步 JavaScript 和 XML). 现在允许浏览器与务器通信 ...
- shell脚本大小写转换
几个方法 1.tr命令 2.sed替换 3.awk的tolower() toupper() 4.perl语言 详见 http://blog.51cto.com/wangxiaoyu/197623 L ...
- getattribute
属性访问拦截器 class Itcast(object): def __init__(self,subject1): self.subject1 = subject1 self.subject2 = ...
- Spring复习
第一天 IOC:控制反转,对象的创建权交给Spring DI:依赖注入,前提必须有IOC的环境,Spring管理这个类的时候将类的依赖的属性注入(设置)进来. 包括集合的注入 ClassPathXml ...
- 二叉树遍历(flist)(已知中序和按层遍历,求先序 )
问题 F: 二叉树遍历(flist) 时间限制: 1 Sec 内存限制: 128 MB提交: 11 解决: 9[提交][状态][讨论版][命题人:quanxing][Edit] [TestData ...