给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。

示例 1:

输入: [   [1,1,1],   [1,0,1],   [1,1,1] ] 输出: [   [1,0,1],   [0,0,0],   [1,0,1] ]

示例 2:

输入: [   [0,1,2,0],   [3,4,5,2],   [1,3,1,5] ] 输出: [   [0,0,0,0],   [0,4,5,0],   [0,3,1,0] ]

进阶:

  • 一个直接的解决方案是使用  O(mn) 的额外空间,但这并不是一个好的解决方案。
  • 一个简单的改进方案是使用 O(m + n) 的额外空间,但这仍然不是最好的解决方案。
  • 你能想出一个常数空间的解决方案吗?

方法一:

class Solution {
public:
void setZeroes(vector<vector<int> >& matrix)
{
int r = matrix.size();
if(r == 0)
return;
int c = matrix[0].size();
vector<int> checkr(r, 0);
vector<int> checkc(c ,0);
for(int i = 0; i < r; i++)
for(int j = 0; j < c; j++)
if(matrix[i][j] == 0)
{
checkr[i] = 1;
checkc[j] = 1;
}
for(int i = 0; i < r; i++)
{
if(checkr[i] == 0)
continue;
for(int j = 0; j < c; j++)
{
matrix[i][j] = 0;
if(checkc[j] == 0)
continue;
for(int k = 0; k < r; k++)
matrix[k][j] = 0;
}
}
}
};

方法二:

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

Leetcode73. 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. [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 ...

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

  5. [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 ...

  6. leetcode 73 矩阵置零 Python

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

  7. LeetCode:矩阵置零【73】

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

  8. Java实现 LeetCode 73 矩阵置零

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

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

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

随机推荐

  1. centos 7 安装dotnet core

    dotnetcore 支持的Linux 版本请看官网:https://docs.microsoft.com/zh-cn/dotnet/core/linux-prerequisites?tabs=net ...

  2. 杂项-公司:AT&T

    ylbtech-杂项-公司:AT&T AT&T公司(英语:AT&T Inc.,原为American Telephone & Telegraph的缩写,也是中文译名美国电 ...

  3. iOS之UITableView加载网络图片cell自适应高度

    #pragma mark- UITableView - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSI ...

  4. 通过数据库中的表,使用 MyEclipse2017的反向生成工具-->hibernate反转引擎引擎(MyEclipse2017自带的插件) 来反转生成实体类和对应的映射文件

    通过数据库中的表,使用 MyEclipse2017的反向生成工具-->hibernate反转引擎引擎(MyEclipse2017自带的插件) 来反转生成实体类和对应的映射文件   文章目录 Ja ...

  5. 表单单选按钮input[type="radio"]

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  6. Pandas系列-读取csv/txt/excel/mysql数据

    本代码演示: pandas读取纯文本文件 读取csv文件 读取txt文件 pandas读取xlsx格式excel文件 pandas读取mysql数据表 import pandas as pd 1.读取 ...

  7. <scrapy爬虫>爬取腾讯社招信息

    1.创建scrapy项目 dos窗口输入: scrapy startproject tencent cd tencent 2.编写item.py文件(相当于编写模板,需要爬取的数据在这里定义) # - ...

  8. Free- Linux必学的60个命令

    1.作用 free命令用来显示内存的使用情况,使用权限是所有用户. 2.格式 free [-b|-k|-m] [-o] [-s delay] [-t] [-V] 3.主要参数 -b -k -m:分别以 ...

  9. 【核心核心】4.Spring【IOC】注解方式

    1.导入jar包 2.创建对应的类 public interface HelloService { public void sayHello(); } /** * @Component(value=& ...

  10. Ubuntu 14.04 Ruby 2.3.3 安装

    在Ubuntu 14.04通过下载Ruby源码包进行安装. 第一步,更新apt-get sudo apt-get update 通过apt-get安装ruby依赖 sudo apt-get insta ...