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. IIR滤波器设计(调用MATLAB IIR函数来实现)

    转载请注明文章来源 – http://blog.csdn.net/v_hyx ,请勿用于任何商业用途         对于滤波器设计,以前虽然学过相关的理论(现代数字信号处理和DSP设计),但一直不求 ...

  2. 一个简单的观察者模式的JS实现

    这不是原创文章,主要是写给自己看的.原文比较详细容易让人,我提取最简单最好理解的部分,便于我以后用到.参考http://www.cnblogs.com/TomXu/archive/2012/03/02 ...

  3. 微信小程序页面跳转的四种方法

    wx.navigateTo({}) ,保留当前页面,跳转到应用内的某个页面,使用 wx.navigateBack 可以返回; 示例: 1 wx.navigateTo({ 2 url:'../test/ ...

  4. [笔记]CodeIgniter的SESSION

         由于HTTP协议本身是无状态的,所以当保留某个用户的访问状态信息时,需要客户端有一个唯一标识传给服务端,这个唯一标识就是SESSION ID,存放在客户端的COOKIE中,然后服务端根据该标 ...

  5. vim自动缩进

    最近写完程序,在进行调试时发现特别困难,代码乱的一塌糊涂,特别是代码量很多时,调试起来特别囧,逻辑很难理清. 这让我想起了缩进功能,可以让代码自动对齐. gedit编辑器在菜单栏里的编辑->首选 ...

  6. iOS NSLog去掉时间戳及其他输出样式

    1.一般项目中我的NSLog会在Prefix.pch文件添加如下代码,已保证在非调试状态下NSLog不工作   1 2 3 4 5 #ifdef DEBUG #define NSLog(...) NS ...

  7. 6-20 No Less Than X in BST(20 分)

    You are supposed to output, in decreasing order, all the elements no less than X in a binary search ...

  8. javascript reg 不加入分组

    from :https://stackoverflow.com/questions/3512471/what-is-a-non-capturing-group-what-does-a-question ...

  9. TypeScript学习笔记(二) - 基本类型

    本篇将简单介绍TypeScript的几种基本类型. TypeScript基本类型有如下几种: Boolean Number String Array Tuple Enum Any 另外还有void类型 ...

  10. test20181021 快速排序

    题意 对于100%的数据,\(n,m \leq 10^5\) 分析 考场上打挂了. 最大值就是后半部分和减前半部分和. 最小是就是奇偶相减. 方案数类似进出栈序,就是catalan数 线段树维护即可, ...