Leetcode 073 Set Matrix Zeroes

时间:2023-02-13 15:06:02

Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

click to show follow up.

Follow up:

Did you use extra space?
A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

1.O(m+n)space,两个数组m和n分别存储第i行和第j列有没有零,比较慢

2.constant space ,利用矩阵的第零行和第零列分别存储第i行和第j列有没有零,需要注意的是再用两个变量row和column来表示第零行和第零列是否有零存在,因为这是边界情况

public class S073 {
public void setZeroes(int[][] matrix) {
//O(m+n) space slow
/* int [] m = new int[matrix.length];
int [] n = new int[matrix[0].length];
for (int i = 0;i<matrix.length;i++) {
for (int j = 0;j<matrix[0].length;j++) {
if (matrix[i][j] == 0) {
m[i] = -1;
n[j] = -1;
}
}
}
for (int i = 0;i<matrix.length;i++) {
if (m[i] == -1) {
for (int j = 0;j<matrix[0].length;j++) {
matrix[i][j] = 0;
if (n[j] == -1) {
for (int k = 0;k<matrix.length;k++) {
matrix[k][j] = 0;
}
}
}
}
} */
//利用矩阵的第零行和第零列分别来存每一列和每一行是否有零存在
int row = -1;
int column = -1;
for (int i = 0;i<matrix.length;i++) {
for (int j = 0;j<matrix[0].length;j++) {
if (matrix[i][j] == 0) {
if (i == 0 ) {
row = 0;
}
if (j == 0) {
column = 0;
}
matrix[0][j] = 0; //第零行
matrix[i][0] = 0; //第零列
}
}
}
for (int i = 1;i<matrix.length;i++) {
if (matrix[i][0] == 0) {
for (int j = 1;j<matrix[0].length;j++) {
matrix[i][j] = 0;
}
}
}
for (int j = 1;j<matrix[0].length;j++) {
if (matrix[0][j] == 0) {
for (int i = 1;i<matrix.length;i++) {
matrix[i][j] = 0;
}
}
}
if (row == 0) {
for (int i = 0;i<matrix[0].length;i++) {
matrix[0][i] = 0;
}
}
if (column == 0) {
for (int i = 0;i<matrix.length;i++) {
matrix[i][0] = 0;
}
}
}
}

Leetcode 073 Set Matrix Zeroes的更多相关文章

  1. Java for LeetCode 073 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. 解题思路: ...

  2. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  3. 073 Set Matrix Zeroes 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...

  4. &lbrack;LeetCode&rsqb; 73&period; 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 ...

  5. Leetcode&num;73 Set Matrix Zeroes

    原题地址 用矩形的第一行和第一列充当mask 代码: void setZeroes(vector<vector<int> > &matrix) { ].empty()) ...

  6. &lbrack;LeetCode&rsqb; 73&period; 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. Follow ...

  7. 【Leetcode】Set Matrix Zeroes

    给定一个m x n的矩阵,如果某个元素为0,则把该元素所在行和列全部置0. Given a m x n matrix, if an element is 0, set its entire row a ...

  8. 【LeetCode】Set Matrix Zeroes 解题报告

    今天看到CSDN博客的勋章换了图表,同一时候也添加显示了博客等级,看起来都听清新的,感觉不错! [题目] Given a m x n matrix, if an element is 0, set i ...

  9. leetcode&lbrack;73&rsqb; Set Matrix Zeroes 将矩阵置零

    给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...

随机推荐

  1. Microservice架构模式简介

    在2014年,Sam Newman,Martin Fowler在ThoughtWorks的一位同事,出版了一本新书<Building Microservices>.该书描述了如何按照Mic ...

  2. Oracle 字符串函数

    平常我们用Oracle主要有两种字符串类型1.char始终为固定的长度,如果设置了长度小于char列的值,则Oracle会自动用空格填充的.当比较char时,Oracle用空格将其填充为等长,再进行比 ...

  3. Codeforces 389B&lpar;十字模拟&rpar;

    Fox and Cross Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submi ...

  4. SQL SERVER 2008 R2配置管理器出现&OpenCurlyDoubleQuote;远程过程调用失败”【0x800706be】的解决办法

    以前SQL Server 2008 不能登陆的时候,总是通过“计算机管理”→“SQL Server服务”更改一下,"SQL Server(MSSQLSERVER)".可是现在出现的 ...

  5. c 函数及指针学习 8

    联合体 1 2 3 4 5 6 7 8 9 10 11 12 13 #include <stdio.h>   union sa     {     double a;     int b; ...

  6. NopCommerce架构分析之六------自定义RazorViewEngine

    系统中对Razor的支持包括两部分,其中之一就是自定义RazorViewEngine 一.自定义RazorViewEngine 在Global.asax.cs的Application_Start方法中 ...

  7. STM32——C语言课堂原代码

    指针 /* ============================================================================ Name : Hello.c Au ...

  8. NodeJS中使用swig模板引擎

    NodeJS中的默认引擎是jade有点过于复杂,而且不是以HTML为基础的,学习成本和前端适应成本都很大.而ejs虽然简单,但不支持模板导入,而且效率一般. swig的语法简单,学习成本很低,符合常规 ...

  9. 浅析Sql Server参数化查询

    说来惭愧,工作差不多4年了,直到前些日子被DBA找上门让我优化一个CPU占用很高的复杂SQL语句时,我才突然意识到了参数化查询的重要性. 相信有很多开发者和我一样对于参数化查询认识比较模糊,没有引起足 ...

  10. 使用IDEA进行Lua代码调试、自动提示、代码跳转、智能重命名

    试了几个Lua IDE后,Lua Studio.Lua Glider.VS+babelua插件.Sublime都不是特别满意.直到发现了国人自创的另一个神奇工具:基于IDEA的EmmyLua插件.该插 ...