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

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?

思路:第一行和第一列用来标示该行、该列是否全0,但事先得判断第一行、第一列是否全0=>用两个额外的变量存储

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(matrix.empty()) return; bool firstLineZero = false;
bool firstColumnZero = false;
if(matrix[][]==){
firstLineZero = true;
firstColumnZero = true;
}
//the first line
for(int i = ; i<matrix[].size(); i++)
{
if(matrix[][i]!=) continue;
firstLineZero = true;
break;
}
//the first column
for(int i = ; i<matrix.size(); i++)
{
if(matrix[i][]!=) continue;
firstColumnZero = true;
break;
} for(int i = ; i < matrix.size(); i++)
{
for(int j = ; j<matrix[].size(); j++)
{
if(matrix[i][j] != ) continue;
matrix[i][] = ;
matrix[][j] = ;
}
} for(int i = ; i<matrix[].size(); i++)
{
if(matrix[][i]!=) continue;
for(int j = ; j<matrix.size(); j++)
{
matrix[j][i]=;
}
}
for(int i = ; i<matrix.size(); i++)
{
if(matrix[i][]!=) continue;
for(int j = ; j<matrix[].size(); j++)
{
matrix[i][j]=;
}
} if(firstLineZero)
{
for(int i = ; i< matrix[].size(); i++)
{
matrix[][i] = ;
}
}
if(firstColumnZero)
{
for(int i = ; i< matrix.size(); i++)
{
matrix[i][] = ;
}
}
}
};

73. Set Matrix Zeroes (Array)的更多相关文章

  1. 【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 ...

  2. 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 ...

  3. Leetcode#73 Set Matrix Zeroes

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

  4. [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. Follow ...

  5. leetcode[73] Set Matrix Zeroes 将矩阵置零

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

  6. 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 ...

  7. 【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. Fo ...

  8. 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. 重点是空间复 ...

  9. 【一天一道LeetCode】#73. Set Matrix Zeroes

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

随机推荐

  1. Alpha冲刺一(7/10)

    前言 队名:拖鞋旅游队 组长博客:https://www.cnblogs.com/Sulumer/p/10013652.html 作业博客:https://edu.cnblogs.com/campus ...

  2. Editor does not contain a main type

    1.错误描述 2.错误原因 在含有main方法的类中,运行应用程序,却提示这个错误:编译器不包含main类型 3.解决办法 (1)选择该Java类上一级文件,build path--->use ...

  3. 词云:解决pip install wordcloud安装过程中报错“error: command 'x86_64-linux-gnu-gcc' failed with exit status 1”问题

    外部环境:ubuntu16.04, 64bits, 全局环境python2.7 在虚拟环境(python3.5)中执行 pip install wordcloud 时安装失败,报错: error: c ...

  4. Sublime text代码补全插件(支持Javascript、JQuery、Bootstrap框架)

    Sublime text代码补全插件(支持Javascript.JQuery.Bootstrap框架)   插件名称:javascript-API-Completions 支持Javascript.J ...

  5. Python ImportError: No module named Image

    /********************************************************************************* * Python ImportEr ...

  6. SQL Server2008 R2开启远程连接总结

    ============================== SQL Server2008 R2开启远程连接(最全总结) ============================== 安装过程:适用W ...

  7. 7-13 求链式线性表的倒数第K项(20 分)

    给定一系列正整数,请设计一个尽可能高效的算法,查找倒数第K个位置上的数字. 输入格式: 输入首先给出一个正整数K,随后是若干正整数,最后以一个负整数表示结尾(该负数不算在序列内,不要处理). 输出格式 ...

  8. flask第二十一篇——练习题

    自定义url转化器 实现一个自定义的URL转换器,这个转换器需要满足的是获取从多少到多少的url,例如,你输入的地址是http://127.0.0.1:8000/1-5/,那么页面返回[1,2,3,4 ...

  9. Jenkins搭建.NET自动编译发布远程环境

    继上一篇文章Jenkins搭建.NET自动编译发布本地环境 发布到本地成功后,接下来配置发布到远程环境. Build配置——发布到远程 根据前面VS中发布项目,生成的CustomProfile2 来配 ...

  10. POI2012题解

    POI2012题解 这次的完整的\(17\)道题哟. [BZOJ2788][Poi2012]Festival 很显然可以差分约束建图.这里问的是变量最多有多少种不同的取值. 我们知道,在同一个强连通分 ...