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. 第九周助教工作总结——NWNU李泓毅

    1.助教博客链接: https://www.cnblogs.com/NWNU-LHY/ 2.作业要求博客链接: https://www.cnblogs.com/nwnu-daizh/p/1072688 ...

  2. ABP的确认框

    使用之前,是需要添加对abp.sweet-alert.js的引用,否则就无法正常使用. 确认框 abp.message.info('some info message', 'some optional ...

  3. Python3.7版本unittest框架添加用例的方法

    1.实例demo是用谷歌浏览器在百度首页搜python的动作脚本:     BaiDu_test是指脚本中自己定义的类名,test_get是指你的类中定义的testcase方法. 在if name函数 ...

  4. (PMP)第3章-----项目经理的角色

    项目经理的能力: 1.技术项目管理 2.领导力 3.战略和商务管理 ----------------------------------------------- 管理:指挥从一个位置到另一个位置 领 ...

  5. Git使用(一、TortoiseGit和Gitlab在Windows下的项目库创建和上传)

    介绍使用TortoiseGit初次创建并上传到gitlab项目库,转载请注明出处. 一.需要先安装git环境,并配置Git用户名及邮箱. 二.用PuTTYgen生成公约私钥对(鼠标画画).PuTTYg ...

  6. ExtJS中listener方法和handler方法的区别

    listener方法和handler方法的区别在文档中的说明的太玄乎了,看不懂 listeners监听能够对一个click Event事件添加任意多个的事件响应处理函数 而handler处理只能够通过 ...

  7. PowerPoint 2013中创建自定义路径动画的方法

    1.在幻灯片中选择对象,在“动画”选项卡的“高级动画”组中单击“添加动画”按钮,在打开的下拉列表中选择“自定义路径”选项,如图1所示. 图1 选择“自定义路径”选项 2.此时鼠标指针变为十字形,在幻灯 ...

  8. eclipse设置新建jsp文件默认字符编码为utf-8

    在使用Eclipse开发中,编码默认是ISO-8859-1,不支持中文.这样我们每次新建文件都要手动修改编码,非常麻烦.其实我们可以设置文件默认编码,今后再新建文件时就不用修改编码了. 1.打开Ecl ...

  9. 关于oracle数据库的小知识

    --查询语句:select 列名/通配符/列别名/表达式 from 表名 (修饰/限制语句)select * from tab;select tname from tab;--指定的列select t ...

  10. 一、Java和JavaScript

    JavaScript诞生于1995年,所以他得叫我一声姐姐,(*^__^*) .当时它的主要任务就是表单验证,在还没JavaScript的时候,进行表单验证的时候必须要把数据提交到服务器,才能进行表单 ...