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?


题目标签:Array

  这道题目给了我们一个矩阵,让我们把所有是0的位置的那一行和那一列都改成0。我们自己改成0的位置就不需要再改0了。题目还要求我们用 O(1) space,所以就不能另外新建matrix来辅助了。这道题目的难点在于,如果遇到一个0,把它的那一行和那一列都改成0的话,对之后的遍历其他位置的判断就造成了影响。我们可以把这 (遇到0就改这一行和这一列)步骤分解成4个步骤。利用第一行和第一列来存信息,根据信息来对剩下的所有位置改0。那么我们首先要把第一行和第一列里有没有0的这个信息保存起来。
  Step 1: 遍历第一行和第一列,设置两个boolean 来保存,如果有0就是true;
  Step 2: 遍历剩下的行和列,如果遇到0,就把对应在第一行和第一列里的相对位置改成0;
  Step 3: 和step 2一样,遍历除了第一行和第一列的所有位置,对于每一个位置,如果第一行和第一列里的相对位置是0的话,把这个位置改成0;
  Step 4: 最后把第一行和第一列也改0,根据开始存的两个boolean 值。
 
 
 

Java Solution:

Runtime beats 23.97%

完成日期:07/23/2017

关键词:Array

关键点:把原题给的十字改0的这一个过程分解成四个步骤来实现

 public class Solution
{
public void setZeroes(int[][] matrix)
{
boolean firstRow = false;
boolean firstCol = false; // iterate the first row and column to see any zeros there
for(int y=0; y<matrix[0].length; y++)
{
if(matrix[0][y] == 0)
{
firstRow = true;
break;
}
} for(int x=0; x<matrix.length; x++)
{
if(matrix[x][0] == 0)
{
firstCol = true;
break;
}
} // iterate rest rows and columns,
//if see any zero, put zero in corresponding position in first row and first column
for(int x=1; x<matrix.length; x++) // rows
{
for(int y=1; y<matrix[0].length; y++) // columns
{
if(matrix[x][y] == 0)
{
matrix[x][0] = 0;
matrix[0][y] = 0;
}
}
} // iterate rest rows and columns again,
// for each position, if corresponding first row or first column has 0, change this to 0
for(int x=1; x<matrix.length; x++)
{
for(int y=1; y<matrix[0].length; y++)
{
if(matrix[x][0] == 0 || matrix[0][y] == 0)
matrix[x][y] = 0;
}
} // check two boolean firstRow and firstCol, and decide need to make first row and first column to 0
if(firstRow)
for(int y=0; y<matrix[0].length; y++)
matrix[0][y] = 0; if(firstCol)
for(int x=0; x<matrix.length; x++)
matrix[x][0] = 0;
}
}

参考资料:

http://www.cnblogs.com/grandyang/p/4341590.html

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 73. Set Matrix Zeros(矩阵赋零)的更多相关文章

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

  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 矩阵赋零

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

  4. leetcode[73] Set Matrix Zeroes 将矩阵置零

    给定一个矩阵,把零值所在的行和列都置为零.例如: 1 2 3 1 3 1 1 1 操作之后变为 1 3 0 0 0 1 1 方法1: 赋值另存一个m*n的矩阵,在原矩阵为零的值相应置新的矩阵行和列为零 ...

  5. Leetcode 54:Spiral Matrix 螺旋矩阵

    54:Spiral Matrix 螺旋矩阵 Given a matrix of m x n elements (m rows, n columns), return all elements of t ...

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

  7. 073 Set Matrix Zeroes 矩阵置零

    给定一个 m x n 的矩阵,如果一个元素为 0 ,则将这个元素所在的行和列都置零.你有没有使用额外的空间?使用 O(mn) 的空间不是一个好的解决方案.使用 O(m + n) 的空间有所改善,但仍不 ...

  8. [LeetCode] Reshape the Matrix 重塑矩阵

    In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new o ...

  9. 【LeetCode】Spiral Matrix(螺旋矩阵)

    这是LeetCode里的第54道题. 题目要求: 给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ...

随机推荐

  1. 多线程:head first Thread.join()

    不使用Thread.join() 测试线程 先上代码: /** * Created by Zero on 2017/8/23. */ public class TestJoin implements ...

  2. hadoop运行wordcount实例,hdfs简单操作

    1.查看hadoop版本 [hadoop@ltt1 sbin]$ hadoop version Hadoop -cdh5.12.0 Subversion http://github.com/cloud ...

  3. kvm 虚拟化的使用

    kvm原理:基于内核空间虚拟化,加载内核模块,来做到虚拟化(简称内核空间).基于qemu连接内核,driver驱动连接kvm的API接口(简称用户空间): hypervisor 管理硬件设备,传统的虚 ...

  4. Thinkphp3.2版本使用163邮箱发(验证码)邮件

    今天忽然想写一个用户修改密码的功能,又没有短信接口,只能选择用邮箱发送验证码啦,穷啊,没办法,哈哈,以下为正文. ------------------------------------------- ...

  5. XtraGrid滚轮翻页

    滚轮翻页与传动的翻页更为方便,经过本人一番探讨与琢磨终于在XtraGrid的GridView中实现了鼠标滚轮翻页. 我新建了一个组件继承原本的GridControl,在组件中添加了一个ImageLis ...

  6. js 递归函数的使用及常用函数

    1.递归函数的使用: 公园里有一堆桃子,猴子每天吃掉一半,挑出一个坏的扔掉,第6天的时候发现还剩1个桃子,问原来有多少个桃子 var peache;function peaches(n) { if ( ...

  7. 再起航,我的学习笔记之JavaScript设计模式28(委托模式)

    ## 委托模式 ### 概念介绍 **委托模式(Entrust): **多个对象接收并处理同一请求,他们将请求委托给另一个对象统一处理请求. ### 利用委托优化循环 如果我们有一个需求需要让用户点击 ...

  8. 【NOIP2016提高组day2】蚯蚓

    那么我们开三个不上升队列, 第一个记录原来的蚯蚓, 第二个记录乘以p的蚯蚓 第三个记录乘以(1-p)的蚯蚓, 在记录每条就要入队列的时间,就可以求出增加的长度 每次比较三个队列的队首,取最大的值x的切 ...

  9. js自执行函数写法

    (1)写法1 (function(){ //函数内容 })() (2)写法2 (function(){ //函数内容 }())

  10. Java中的类型擦除与桥方法

    类型擦除 Java在语法中虽然存在泛型的概念,但是在虚拟机中却没有泛型的概念,虚拟机中所有的类型都是普通类.无论何时定义一个泛型类型,编译后类型会被都被自动转换成一个相应的原始类型. 比如这个类 pu ...