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(m n) 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.
思路:因为,数组中值为0的元素可能不止一个,所以,常规的思路是,遍历数组,记下值为0元素的行、列数,然后根据其所在的记下的行、列将对应行所在的值全赋值为0,但这样会需要O(m+n)的额外空间。所以换一种思路,我们可以遍历数组,若遇到值为0,则将其对应的第一行、第一列的值赋为0,最后根据第一行、第一列的值,更新数组。这样做存在一个问题,那就是,若第一行、第一列中原本就存在0,此时需要更新数组时,如何区分?我们可以先确定第一行、第一列是否存在0,然后,更新完剩下的数组以后,根据是否存在0,决定第一行、第一列的是否全部更新为0。代码如下:
 class Solution {
public:
void setZeroes(vector<vector<int> > &matrix)
{
int row=matrix.size();
int col=matrix[].size(); if(row==||col==) return; bool rFlag=false,cFlag=false;
for(int i=;i<row;++i)
{
if(matrix[i][]==)
{
rFlag=true;
break;
}
}
for(int i=;i<col;++i)
{
if(matrix[][i]==)
{
cFlag=true;
break;
}
} for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(matrix[i][j]==)
{
matrix[i][]=;
matrix[][j]=;
}
}
} for(int i=;i<row;++i)
{
for(int j=;j<col;++j)
{
if(matrix[i][]==||matrix[][j]==)
matrix[i][j]=;
}
} if(rFlag)
{
for(int i=;i<row;++i)
matrix[i][]=;
}
if(cFlag)
{
for(int i=;i<col;++i)
matrix[][i]=;
} }
};

代码参考了Gradyang的博客

[Leetcode] set matrix zeroes 矩阵置零的更多相关文章

  1. 073 Set Matrix Zeroes 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...

  2. [LeetCode] 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 ...

  3. Leetcode73. Set Matrix Zeroes矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输 ...

  4. [CareerCup] 1.7 Set Matrix Zeroes 矩阵赋零

    1.7 Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column are ...

  5. 【python】Leetcode每日一题-矩阵置零

    [python]Leetcode每日一题-矩阵置零 [题目描述] 给定一个 m x n 的矩阵,如果一个元素为 0 ,则将其所在行和列的所有元素都设为 0 .请使用 原地 算法. 进阶: 一个直观的解 ...

  6. leetcode刷题-73矩阵置零

    题目 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [  [1,1,1],  [1,0,1],  [1,1,1]]输出: ...

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

  8. LeetCode:矩阵置零【73】

    LeetCode:矩阵置零[73] 题目描述 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [   [1,1,1],   ...

  9. Java实现 LeetCode 73 矩阵置零

    73. 矩阵置零 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ...

随机推荐

  1. 如何改变memcached默认的缓存时间?

    我们在使用php的memcached的扩展来对memcached进行数据添加时,数据的有效时间有两种方式.如下图. 至于设置一个UNIX时间戳或      以秒为单位的整数(从当前算起的时间差)来说明 ...

  2. is 和 isinstance的区别 and issubclass

    定义一个子类和父类 class A: pass class B(A): pass is print(type(b) is B) # 结果: True print(type(b) is A) # 结果: ...

  3. 洛谷 U45568 赌神:决斗

    题目描述 \mathcal{tomoo}tomoo决定与\mathcal{CYJian}CYJian进行决斗! 已知\mathcal{tomoo}tomoo有\mathcal{N}N张扑克牌,每张扑克 ...

  4. Linux命令备忘录:mount用于加载文件系统到指定的加载点

    mount命令用于加载文件系统到指定的加载点.此命令的最常用于挂载cdrom,使我们可以访问cdrom中的数据,因为你将光盘插入cdrom中,Linux并不会自动挂载,必须使用Linux mount命 ...

  5. 一个新晋IT行业的努力Duiker

      亲爱的朋友,你好!   我很开心能以这么一篇博客来开始我的IT努力之路.我叫Duiker,是一名软件工程专业的学生,想通过写博客来提升自己,充实自我. 首先,我要确立自己的学习编程目标: 1.将算 ...

  6. Infinite Maze CodeForces - 196B

    We've got a rectangular n × m-cell maze. Each cell is either passable, or is a wall (impassable). A ...

  7. 003---设计首页index页面

    在项目的urls.py文件添加一条url from django.contrib import admin from django.urls import path, re_path from app ...

  8. hdu 1394 Minimum Inversion Number(线段树)

    参考:http://blog.sina.com.cn/s/blog_691ce2b70101ldmm.html https://blog.csdn.net/wiking__acm/article/de ...

  9. SIMD数据并行(四)——三种结构的比较

    在计算机体系中,数据并行有两种实现路径:MIMD(Multiple Instruction Multiple Data,多指令流多数据流)和SIMD(Single Instruction Multip ...

  10. 13 ThreadLocal

    ThreadLocal 在多线程环境下,每个线程都有自己的数据.一个线程使用自己的局部变量比使用全局变量好,因为局部变量只有线程自己能看见,不会影响其他线程,而全局变量的修改必须加锁. 1. 使用函数 ...