import java.util.Arrays; /**
* Source : https://oj.leetcode.com/problems/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.
*
*
* 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?
*
*/
public class SetMatrixsZero { /**
* 将原矩阵中为0元素所在的行列set为0
* 使用不同空间的算法
* O(mn):复制一个和原来矩阵一样的矩阵,遍历原矩阵,遇到为0的元素,设置复制矩阵的行列为0
* O(m+n):使用一个row[m]记录0至(m-1)行需要置为0的行,一个col[n]数组记录0至(n-1)列需要被置为0的列,最后遍历两个数组对相应行列置位
* O(1):因为如果某一个位置出现了0,那么该行和该列都要被置为0,所以该行的第一列最后会变为0,也就是会抹除原来的数字,
* 该列的第一行也类似,所以第一行和第一列可以代替上面这种方法中的两个数组,用来记录该行该列需要被置为0的情况,
* 但是第0行和第0列需要额外的两个变量来记录,所以占用空间是常数(利用了原矩阵中的第0行和第0列)
*
* 下面实现占用常数空间的方法
*
* @param matrix
* @return
*/
public int[][] findZero (int[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) {
return matrix;
}
int firstRow = 1;
int firstCol = 1;
// 判断第0行和第0列
for (int i = 0; i < matrix.length; i++) {
if (matrix[i][0] == 0) {
firstCol = 0;
break;
}
}
for (int i = 0; i < matrix[0].length; i++) {
if (matrix[0][i] == 0) {
firstRow = 0;
break;
}
} // 遍历第1-(m-1)行和1-(n-1)列
for (int i = 1; i < matrix.length; i++) {
for (int j = 1; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
matrix[i][0] = 0;
matrix[0][j] = 0;
}
}
}
setZero(matrix, firstRow, firstCol);
return matrix;
} /**
*
* @param matrix
* @param firstRow
* @param firstCol
*/
public void setZero (int[][] matrix, int firstRow, int firstCol) {
for (int i = 1; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][0] == 0 || matrix[0][j] == 0) {
matrix[i][j] = 0;
}
}
}
if (firstCol == 0) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}
if (firstRow == 0) {
for (int i = 0; i < matrix[0].length; i++) {
matrix[0][i] = 0;
}
}
} public static void printMatrix (int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
System.out.println(Arrays.toString(matrix[i]));
}
System.out.println();
} public static void main(String[] args) {
SetMatrixsZero setMatrixsZero = new SetMatrixsZero();
int[][] arr = new int[][]{};
int[][] arr1 = new int[][]{
{1}
};
int[][] arr2 = new int[][]{
{0}
};
int[][] arr3 = new int[][]{
{1,0,1}
};
int[][] arr4 = new int[][]{
{1},
{0},
{1}
};
int[][] arr5 = new int[][]{
{1,2,3},
{0,3,4},
{1,0,4}
}; printMatrix(setMatrixsZero.findZero(arr));
printMatrix(setMatrixsZero.findZero(arr1));
printMatrix(setMatrixsZero.findZero(arr2));
printMatrix(setMatrixsZero.findZero(arr3));
printMatrix(setMatrixsZero.findZero(arr4));
printMatrix(setMatrixsZero.findZero(arr5));
}
}

leetcode — set-matrix-zeroes的更多相关文章

  1. LeetCode: Set Matrix Zeroes 解题报告

    Set Matrix ZeroesGiven a m x n matrix, if an element is 0, set its entire row and column to 0. Do it ...

  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. [leetcode]Set Matrix Zeroes @ Python

    原题地址:https://oj.leetcode.com/problems/set-matrix-zeroes/ 题意:Given a m x n matrix, if an element is 0 ...

  4. LeetCode OJ--Set Matrix Zeroes **

    http://oj.leetcode.com/problems/set-matrix-zeroes/ 因为空间要求原地,所以一些信息就得原地存储.使用第一行第一列来存本行本列中是否有0.另外对于第一个 ...

  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. 原题链接:h ...

  6. LeetCode Set Matrix Zeroes(技巧+逻辑)

    题意: 给一个n*m的矩阵,如果某个格子中的数字为0,则将其所在行和列全部置为0.(注:新置的0不必操作) 思路: 主要的问题是怎样区分哪些是新来的0? 方法(1):将矩阵复制多一个,根据副本来操作原 ...

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

  8. LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors

    1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...

  9. Leetcode 细节实现 Set Matrix Zeroes

    Set Matrix Zeroes Total Accepted: 18139 Total Submissions: 58671My Submissions Given a m x n matrix, ...

  10. 【LeetCode】73. Set Matrix Zeroes (2 solutions)

    Set Matrix Zeroes Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do i ...

随机推荐

  1. Python Day 10

    阅读目录: 函数 函数的使用 函数的分类 函数的返回值 ##函数 ##函数的定义-----what?----什么是函数: # 函数:完成 特定 功能的代码块,作为一个整体,对其进行特定的命名,该名字就 ...

  2. go 函数类型

    在go中,函数也可以被当成数据类型 e.g:下面有两个函数,+.-,然后定义了一个函数类型FuncType1,然后对funcType1附于不同的函数,则funcType1就可以执行相应的函数 pack ...

  3. 如何在已安装Python解释器的Linux上更新Python

    在Linux环境下升级Python (附:解决pip报错 subprocess.CalledProcessError: Command '('lsb_release', '-a')' returned ...

  4. php签名认证

    一.概述 开年第一篇,该篇主要讲述了接口开发中,如何安全认证.如何用php签名认证. 二.说说历史 签名认证是什么?为什么要做签名认证?签名认证哪里会用到?no.no.no.....是不是,是不是,一 ...

  5. Centos7 网络报错Job for iptables.service failed because the control process exited with error code.

    今天在进行项目联系的时候,启动在待机的虚拟机,发现虚拟机的网络设置又出现了问题. 我以为像往常一样重启网卡服务就能成功,但是它却报了Job for iptables.service failed be ...

  6. noip第31课资料

  7. OC字典的使用

    在OC中,字符串.数组.字典是最常见的对象类型,但是在这三个当中,字典的用法相对较少,因为字典的属性和方法比较少,但是一个字典的用法比较复杂,因为在一个字典当中,既可以包含字符串,也可以包含数组,数组 ...

  8. Shell输入和输出功能-3

  9. Linux上搭建Hadoop集群

    本文将为初学者的搭建简单的伪分布式集群,将搭建一台虚拟机,用于学习Hadoop 工具:vm虚拟机,centOS7,jdk-8,Hadoop2.7,xftp,xshell 用户:在虚拟机中创建一个had ...

  10. Calendar and GregorianCalendar

    1.GregorianCalendar是Calendar的一个具体子类,提供了世界上大多数国家/地区使用的标准日历系统 2.注意 (1)月份:1月到12月[0-11] (2)星期:周日到周六[1-7] ...