博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Set Matrix Zeroes
阅读量:4074 次
发布时间:2019-05-25

本文共 1214 字,大约阅读时间需要 4 分钟。

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

Java代码:

public class Solution {    public void setZeroes(int[][] matrix) {        boolean firstColZero = false, firstRowZero = false;        for(int i = 0;i < matrix.length;i++)            if(matrix[i][0] == 0)                firstColZero = true;        for(int j = 0;j < matrix[0].length;j++)            if(matrix[0][j] == 0)                firstRowZero = true;        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] = matrix[0][j] = 0;        for(int i = 1;i < matrix.length;i++)            if(matrix[i][0] == 0)                for(int j = 0;j < matrix[0].length;j++)                    matrix[i][j] = 0;        for(int j = 1;j < matrix[0].length;j++)            if(matrix[0][j] == 0)                for(int i = 0;i < matrix.length;i++)                    matrix[i][j] = 0;        if(firstColZero)            for(int i = 0;i < matrix.length;i++)                matrix[i][0] = 0;        if(firstRowZero)            for(int j = 0;j < matrix[0].length;j++)                matrix[0][j] = 0;    }}
 

转载地址:http://diuni.baihongyu.com/

你可能感兴趣的文章
HOLOTOOLKIT的使用
查看>>
Unity Camera的两种模式
查看>>
Collider Collision 区别
查看>>
博客推荐
查看>>
UNITY3D中涉及的一些数学知识
查看>>
Unity3D粒子系统 合集
查看>>
UNITY在VS中调试
查看>>
Unity StartCoroutine 和 yield return 深入研究
查看>>
Unity-Animator深入系列
查看>>
UNITY3D的变量初始化问题
查看>>
UNITY3D单词学习 speed和velocity的区别
查看>>
Unity3D中的线性插值Lerp()函数解析
查看>>
pitch yaw roll 的区别
查看>>
什么是UV?
查看>>
手机开启HDR后拍照有什么不同?
查看>>
图文详解Unity3D中Material的Tiling和Offset是怎么回事
查看>>
voxel 与 pixel
查看>>
vector3.forward和transform.forward的区别!
查看>>
HOLOLENS的空间管理
查看>>
unity3d 的Quaternion.identity和transform.rotation区别是什么
查看>>