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. Javascript实现重力弹跳拖拽运动效果

    声明: By:GenialX 个人主页:胡旭博客 - www.ihuxu.com QQ:2252065614 演示地址: http://www.ihuxu.com/project/gcdmove/ 调 ...

  2. Alpha冲刺 (4/10)

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

  3. react中map循环中key取值问题

    数组中没有唯一的key,可以定义变量,拼接字符串连接. let i = 0; key={`infoItem-col-${i++}`}

  4. EasyNVR H5无插件RTSP直播方案在Windows server 2012上修复无法定位GetNumaNodeProcessorMaskEx的问题

    今天遇到一个客户在使用EasyNVR无插件安防直播解决方案的时候,在Windows Server 2012上出现一个问题提示: 经过反复的查找,虽然提示上显示问题出在KERNEL32.dll上,但是已 ...

  5. Zabbix(Windows)

    Windows环境下只能装客户端, 而不能装服务端; zabbix官网 https://www.zabbix.com/ 下载并解压到本地 修改配置文件: # Hostname=Hostname=DES ...

  6. (转)List<T>的各种排序方法

    近日,在工作的时候遇到要对一个大的List<T>集合进行排序,于是就了解下各种List<T>的排序方法. 首先,排序自然就会想到用Sort方法,看看List<T>的 ...

  7. 【转】嵌入式Linux启动配置文件及脚本

    原文网址:http://blog.csdn.net/shuaishuai80/article/details/6202497 使用Busybox制作根文件系统时,/etc目录非常重要,它包含了嵌入式L ...

  8. linux之 CentOS/RHEL/Scientific Linux 6 & 7上安装Telnet

    声明: 在安装和使用Telnet之前,需要记住以下几点. 在公网(WAN)中使用Telnet是非常不好的想法.它会以明文的格式传输登入数据.每个人都可以看到明文.如果你还是需要Telnet,强烈建议你 ...

  9. nginx rewrite规则实例讲解

    一.正则表达式匹配,其中: * ~ 为区分大小写匹配* ~* 为不区分大小写匹配* !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 二.文件及目录匹配,其中:* -f和!-f用来判断是否存在文 ...

  10. bzoj1925(SCOI2010)地精部落

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1925 要怎样才能想出正解呢? 当然有一维表示从1到 i . 发现最后是递增的方案数=最后是递 ...