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 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?

解法一:

使用数组分别记录需要置零的行列。然后根据数组信息对相应行列置零。

空间复杂度O(m+n)

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(matrix.empty() || matrix[].empty())
return; int m = matrix.size();
int n = matrix[].size(); vector<bool> row(m, false);
vector<bool> col(n, false); for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(matrix[i][j] == )
{
row[i] = true;
col[j] = true;
}
}
} for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(row[i] == true)
matrix[i][j] = ;
if(col[j] == true)
matrix[i][j] = ;
}
}
}
};

解法二:

使用第一行和第一列记录该行和该列是否应该置零。

对于由此覆盖掉的原本信息,只要单独遍历第一行第一列判断是否需要置零即可。

空间复杂度O(1)

class Solution {
public:
void setZeroes(vector<vector<int> > &matrix) {
if(matrix.empty() || matrix[].empty())
return;
int m = matrix.size();
int n = matrix[].size();
bool col0 = false;
bool row0 = false;
for(int i = ; i < m; i ++)
{
if(matrix[i][] == )
{
col0 = true;
break;
}
}
for(int j = ; j < n; j ++)
{
if(matrix[][j] == )
{
row0 = true;
break;
}
}
for(int i = ; i < m; i ++)
{
for(int j = ; j < n; j ++)
{
if(matrix[i][j] == )
{
matrix[][j] = ;
matrix[i][] = ;
}
}
}
for(int i = ; i < m; i ++)
{
if(matrix[i][] == )
{
for(int j = ; j < n; j ++)
{
matrix[i][j] = ;
}
}
}
for(int j = ; j < n; j ++)
{
if(matrix[][j] == )
{
for(int i = ; i < m; i ++)
{
matrix[i][j] = ;
}
}
}
if(col0 == true)
{
for(int i = ; i < m; i ++)
{
matrix[i][] = ;
}
}
if(row0 == true)
{
for(int j = ; j < n; j ++)
{
matrix[][j] = ;
}
}
}
};

【LeetCode】73. Set Matrix Zeroes (2 solutions)的更多相关文章

  1. 【LeetCode】-- 73. Set Matrix Zeroes

    问题描述:将二维数组中值为0的元素,所在行或者列全set为0:https://leetcode.com/problems/set-matrix-zeroes/ 问题分析:题中要求用 constant ...

  2. 【LeetCode】73. Set Matrix Zeroes 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 原地操作 新建数组 队列 日期 题目地址:https ...

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

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

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

  5. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  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】867 - Transpose Matrix

    [题干描述] Given a matrix A, return the transpose of A. The transpose of a matrix is the matrix flipped ...

  8. 【LeetCode】766. Toeplitz Matrix 解题报告

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:两两比较 方法二:切片相等 方法三:判断每条 ...

  9. 【LeetCode】59. Spiral Matrix II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 维护四个边界和运动方向 保存已经走过的位置 日期 题 ...

随机推荐

  1. keras实现mnist数据集手写数字识别

    一. Tensorflow环境的安装 这里我们只讲CPU版本,使用 Anaconda 进行安装 a.首先我们要安装 Anaconda 链接:https://pan.baidu.com/s/1AxdGi ...

  2. iOS开发系列--音频播放、录音、

    音频 在iOS中音频播放从形式上可以分为音效播放和音乐播放.前者主要指的是一些短音频播放,通常作为点缀音频,对于这类音频不需要进行进度.循环等控制.后者指的是一些较长的音频,通常是主音频,对于这些音频 ...

  3. html5开发<video>视频字幕的程序

    <!DOCTYPE html> <html lang="en"> <head>     <meta charset="utf-8 ...

  4. Get buck-boost performance from a boost regulator

    The SEPIC (single-ended, primary-inductance-converter) topology is generally a good choice for volta ...

  5. PHP正则表达式30分钟入门教程

    正则表达式30分钟入门教程 三个常用的知识点: 1.惰性匹配:正则引擎默认是贪婪的,若要最少重复的话,需要用到惰性匹配符 “?” 懒惰限定符 代码/语法 说明 *? 重复任意次,但尽可能少重复 +? ...

  6. vi命令用法

    从shell中启动可视化编辑器vi filename指示shell启动vi编辑器,并将参数filename传给它.如果当前目前中存在该文件,则vi编辑器将它解释为要打开的文件:如果没有该文件,则vi编 ...

  7. poj3126--Prime Path(广搜)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11751   Accepted: 6673 Descr ...

  8. A4纸的象素分辨率计算[转]

    在公制长度单位与屏幕分辨率进行换算时,必须用到一个DPI(Dots Per Inch)指标.在Windows系统的网页打印中默认采用的是96dpi,Mac系统中默认的是72dpi. A4纸张的尺寸是2 ...

  9. 创建一个简单的windows服务,每间隔一定时间重复执行批处理文件

    创建一个windows服务项目,增加App.config <?xml version="1.0" encoding="utf-8" ?> <c ...

  10. log4net 自定义Appender

    最近有个需求,使用log4net来记录日志,然后将数据保存到服务器端.一开始打算写一个windows service,定期上传日志. 后来又因为一些场景下不适应,因此直接改为保存内存中,到一定阀值之后 ...