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. 201621123010《Java程序设计》第8周学习总结

    1. 本周学习总结 以你喜欢的方式(思维导图或其他)归纳总结集合相关内容. 2. 书面作业 1. ArrayList代码分析 1.1 解释ArrayList的contains源代码 答:如图,可见co ...

  2. tensorflow命令行参数:tf.app.flags.DEFINE_string、tf.app.flags.DEFINE_integer、tf.app.flags.DEFINE_boolean

    tf 中定义了 tf.app.flags.FLAGS ,用于接受从终端传入的命令行参数,相当于对Python中的命令行参数模块optpars(参考:python中处理命令行参数的模块optpars)做 ...

  3. Linux udhcp client (udhcpc) get IP at anytime

    /*************************************************************************************** * Linux udh ...

  4. sublime text2下配置c++

    今天安装了sublime text2,真是编辑神器,不再用notepad了. 笔记本上没有c++运行环境,用编辑器既轻巧,又方便,VS太臃肿了. 要在sublime text2 下运行c++程序,需要 ...

  5. 在Linux中批量修改字符串的命令

    昨天一个朋友忽然问我,在Linux下如何批量修改字符串,当时瞬间懵逼了,完全想不起来....... 今天特意的重温了一下Linux下的一些常用命令,并将这个遗忘的批量修改字符串的命令记录下来(资料来自 ...

  6. Idea问题记录

    1.warning提示idea found duplicate code 打开 Settings → Editor → Inspections. 在出现的搜索栏处搜索 Duplicated Code ...

  7. 2018-2019-2 《网络对抗技术》Exp3免杀原理与实践 20165222

    1. 实践内容  1.1 正确使用msf编码器 使用 msfvenom -p windows/meterpreter/reverse_tcp -e x86/shikata_ga_nai -i 7 -b ...

  8. 零基础学Cocos2d-X 3.0 - 04

    忙完两个项目后.最终有时间继续学习Cocos2d-X 了. 常听人说.Cocos2d-X 有四个类是最经常使用的,包含: Director 类----> 导演 Scene 类 -----> ...

  9. Linux下利用Shell使PHP并发采集淘宝产品

    上次项目中用到<<PHP采集淘宝商品>> 此方法有一个缺点,就是执行效率问题.一个商品采集平均需要0.8秒.那10000个商品采集完需要2个半小时. 首先想到的解决办法是并发. ...

  10. Webpack-simple cross-env 不是内部或外部命令问题处理

    本文转载自:https://www.cnblogs.com/stono/p/6984222.html Webpack-simple cross-env 不是内部或外部命令问题处理 学习了:https: ...