给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零。
你有没有使用额外的空间?
使用 O(mn) 的空间不是一个好的解决方案。
使用 O(m + n) 的空间有所改善,但仍不是最好的解决方案。
你能设计一个使用恒定空间的解决方案吗?
详见:https://leetcode.com/problems/set-matrix-zeroes/description/

Java实现:

class Solution {
public void setZeroes(int[][] matrix) {
int m=matrix.length;
int n=matrix[0].length;
boolean rowZero=false;
boolean colZero=false;
for(int j=0;j<n;++j){
if(matrix[0][j]==0){
rowZero=true;
}
}
for(int i=0;i<m;++i){
if(matrix[i][0]==0){
colZero=true;
}
}
for(int i=1;i<m;++i){
for(int j=1;j<n;++j){
if(matrix[i][j]==0){
matrix[i][0]=0;
matrix[0][j]=0;
}
}
}
for(int i=1;i<m;++i){
for(int j=1;j<n;++j){
if(matrix[i][0]==0||matrix[0][j]==0){
matrix[i][j]=0;
}
}
}
if(rowZero){
for(int j=0;j<n;++j){
matrix[0][j]=0;
}
}
if(colZero){
for(int i=0;i<m;++i){
matrix[i][0]=0;
}
}
}
}

参考:https://www.cnblogs.com/grandyang/p/4341590.html

073 Set Matrix Zeroes 矩阵置零的更多相关文章

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

  2. Leetcode73. Set Matrix Zeroes矩阵置零

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

  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. codeforces 660D D. Number of Parallelograms(计算几何)

    题目链接: D. Number of Parallelograms time limit per test 4 seconds memory limit per test 256 megabytes ...

  2. Windows下使用vim编写代码,使用nmake编译代码,使用vs来调试代码

    1.编写代码 2.编写Makefile,如果要调试, 2.1.需要在编译的时候加上/Zi ( Generates complete debugging information),编译由cl.exe来完 ...

  3. loading bar

    上面的loading条,想到的办法是用两个半圆覆盖实现,结果也就这么做了,可是明明一个圆就可以的,哎智商堪忧... <!DOCTYPE html> <html lang=" ...

  4. hdfs 查看报告--命令(hdfs dfsadmin -report)

    [hadoop@master sbin]$ hdfs dfsadmin -reportConfigured Capacity: 8202977280 (7.64 GB)Present Capacity ...

  5. bzoj 2946 公共串 —— 后缀自动机

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2946 建出 n-1 个后缀自动机一起跑呗. 代码如下: #include<cstdio ...

  6. JVM中的Hello World运行机制:

    栗子: package zagoo; public class HelloWorld { public static String HELLOWORLD="Hello World" ...

  7. win10 ObservableCollection 排序自动收缩问题

    ObservableCollection本身是没有排序Sort功能的,不过我们可以通过冒泡排序来实现,以下是扩展功能: public static void Sort<T>(this Ob ...

  8. QTreeWidget笔记

    1.QTreeWidget继承自QTreeView. 2.头文件:QTreeWidget 3.简单使用: #include "mainwindow.h" #include < ...

  9. centos6.5编译安装php7,及配置与nginx通信。

    一.配置编译环境 yum update && yum upgrade yum groupinstall "Development Tools" yum instal ...

  10. 2017年第八届蓝桥杯国赛试题(JavaA组)

    1.结果填空 (满分19分)2.结果填空 (满分47分)3.代码填空 (满分21分)4.程序设计(满分35分)5.程序设计(满分79分)6.程序设计(满分99分) 1.标题:图书排列 将编号为1~10 ...