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. php 微信客服信息推送失败 微信重复推送客服消息 40001 45047

    /*** * 微信客服发送信息 * 微信客服信息推送失败 微信重复推送客服消息 40001 45047 * 递归提交到微信 直到提交成功 * @param $openid * @param int $ ...

  2. ThinkPHP框架目录的介绍

    library目录 Think目录 mvc

  3. ZooKeeper(2)-安装和配置

    一.下载 https://zookeeper.apache.org/ 二.本地模式安装 1.安装前准备 (1)安装Jdk (2)拷贝Zookeeper安装包到Linux系统下 (3)解压到指定目录 . ...

  4. 解决如下出错:DeprecationWarning: Passing 1d arrays as data is deprecated in 0.17 and willraise ValueError in 0.19.

    背景:在Spyder中写几行脚本,目的是应用sklearn自带的svm(支持向量机)算法,来对其自带的digits(手写体数字)数据集进行分类,过程包括训练阶段和预测阶段.将手写体数字数据的特征数据d ...

  5. Python3爬虫(三)请求库的使用之urllib

    Infi-chu: http://www.cnblogs.com/Infi-chu/ 一.urllib库: 1. 是Python内置的HTTP请求库 2. 在Python2中,由urllib和urll ...

  6. YSZOJ:#247. [福利]可持久化线段树 (最适合可持久化线段树入门)

    题目链接:https://syzoj.com/problem/247 解题心得: 可持久化线段树其实就是一个线段树功能的加强版,加强在哪里呢?那就是如果一颗普通的线段树多次修改之后还能知道最开始的线段 ...

  7. Ubuntu14.04安装opencv2.4.13

    本文参考相关链接:http://blog.csdn.net/honyniu/article/details/46390097   系 统:Ubuntu 14.04 x64 opencv版本:2.4.1 ...

  8. windows下subversion服务器搭建

    一.下载subversion服务器端和客户端软件 1.subversion下载地址:http://subversion.tigris.org/ 2.svn比较流行的客户端Tortoisesvn下载地址 ...

  9. javascript对象转为字符串

    function getStringTime(time){ //年 year = time.getFullYear(); //月 month = time.getMonth() if(String(m ...

  10. kettle 遇到 解决Incorrect integer value: '' for column 'id' at row 1 完美解决-费元星

    最近自己在测试一个开源的程序,测试中发现.该程序都添加和更新的时候回出现 Incorrect integer value: '' for column 'id' at row 1类是的错误! 后来我自 ...