Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.

click to show follow up.

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 Solution {
public void setZeroes(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
int row = 0, col = 0;
boolean sig = true;
for(int i = 0; i < matrix.length; i ++){
for(int j = 0; j < matrix[0].length; j ++){
if(matrix[i][j] == 0){
if(sig){
row = i;
col = j;
sig = false;
} else {
matrix[row][j] = 0;
matrix[i][col] = 0;
}
}
}
}
if(sig) return;
for(int i = 0; i < matrix.length; i ++){
for(int j = 0; j < matrix[0].length; j ++){
if((matrix[row][j] == 0 || matrix[i][col] == 0) && i != row && j != col) matrix[i][j] = 0;
}
}
for(int i = 0; i < matrix.length; i ++){
matrix[i][col] = 0;
}
for(int j = 0; j < matrix[0].length; j ++){
matrix[row][j] = 0;
}
}
}

Set Matrix Zeroes的更多相关文章

  1. 55. Set Matrix Zeroes

    Set Matrix Zeroes (Link: https://oj.leetcode.com/problems/set-matrix-zeroes/) Given a m x n matrix, ...

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

  3. Leetcode 细节实现 Set Matrix Zeroes

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

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

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

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

  7. 【leetcode】Set Matrix Zeroes(middle)

    Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. 思路:不能用 ...

  8. Set Matrix Zeroes leetcode java

    题目: Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. cl ...

  9. LeetCode 笔记系列15 Set Matrix Zeroes [稍微有一点hack]

    题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Fol ...

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

随机推荐

  1. Oracle 错误代码

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...

  2. 微信分享朋友链接显示js代码

    通常自己做的一个页面想通过微信像朋友分享时,展示的标题和描述都是不是自己想要的,自己查了一些资料,原来是通过js来进行控制 展示效果如下: 标题.描述.还有分享的图片都是有js来控制的. js代码如下 ...

  3. DB2递归查询

    斐波纳契数列,又称黄金分割数列,指的是这样一个数列:1.1.2.3.5.8.13.21.……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n&g ...

  4. viewPager+Handler+Timer简单实现广告轮播效果

    基本思想是在Avtivity中放一个ViewPager,然后通过监听去实现联动效果,代码理由详细的解释,我就不说了. MainActivity.java package com.example.adm ...

  5. ssh公钥免密码登录

    1,生成公钥 ssh-keygen -t rsa 2,上传至服务器 将个人电脑的公钥添加到服务器 cat id_rsa.pub >> ~/.ssh/authorized_keys 3,本地 ...

  6. linux内核打印"BUG: scheduling while atomic

    linux内核打印"BUG: scheduling while atomic"和"bad: scheduling from the idle thread"错误 ...

  7. NSNumber、NSValue、NSDate、NSObject

    注:OC中数组和字典只能存储OC对象不能存放基本数据类型. NSNumber NSNumber可以用来把一个基本数据类型包装成一个NSNumber类型的对象. NSNumber *number = [ ...

  8. 转载 SQL Server 2008 R2 事务与隔离级别实例讲解

    原文:http://blog.itpub.net/13651903/viewspace-1082730/ 一.事务简介 SQL Server的6个隔离级别中有5个是用于隔离事务的,它们因而被称作事务隔 ...

  9. JIRA安装过程中链接mysql的问题!

    测试下我使用的是mysql7.5的版本,JIRA是6.3.6!这是版本引起的问题! 服务器上原生的mysql驱动jar包:Mysql-connector-java-5.1.18-bin 可参考:htt ...

  10. 在FreeBSD上搭建Mac的文件及time machine备份服务

    上周将工作用电脑由公司配备的台式机切换到自己低配的macbook air上面,小本本的128G SSD远远不能满足工作的储存需要,但又不舍得入手昂贵的AirPort Time Capsule,于是考虑 ...