原题链接: http://oj.leetcode.com/problems/set-matrix-zeroes/ 
这是一个矩阵操作的题目,目标非常明白,就是假设矩阵假设有元素为0,就把相应的行和列上面的元素都置为0。这里最大的问题就是我们遇到0的时候不能直接把矩阵的行列在当前矩阵直接置0,否则后面还没訪问到的会被当成原来是0,最后会把非常多不该置0的行列都置0了。
一个直接的想法是备份一个矩阵,然后在备份矩阵上推断,在原矩阵上置0,这样当然是能够的,只是空间复杂度是O(m*n),不是非常理想。
上面的方法怎样优化呢?我们看到事实上推断某一项是不是0仅仅要看它相应的行或者列应不应该置0就能够,所以我们能够维护一个行和列的布尔数组,然后扫描一遍矩阵记录那一行或者列是不是应该置0就可以,后面赋值是一个常量时间的推断。这个方案的空间复杂度是O(m+n)。
事实上还能够再优化,我们考虑使用第一行和第一列来记录上面所说的行和列的置0情况,这里问题是那么第一行和第一列自己怎么办?想要记录它们自己是否要置0,仅仅须要两个变量(一个是第一行,一个是第一列)就能够了。然后就是第一行和第一列,假设要置0,就把它的值赋成0(反正它终于也该是0,不管第一行或者第一列有没有0),否则保留原值。然后依据第一行和第一列的记录对其它元素进行置0。最后再依据前面的两个标记来确定是不是要把第一行和第一列置0就能够了。这种做法仅仅须要两个额外变量,所以空间复杂度是O(1)。
时间上来说上面三种方法都是一样的,须要进行两次扫描,一次确定行列置0情况,一次对矩阵进行实际的置0操作,所以总的时间复杂度是O(m*n)。代码例如以下: 
public void setZeroes(int[][] matrix) {
if(matrix==null || matrix.length==0 || matrix[0].length==0)
return;
boolean rowFlag = false;
boolean colFlag = false;
for(int i=0;i<matrix.length;i++)
{
if(matrix[i][0]==0)
{
colFlag = true;
break;
}
}
for(int i=0;i<matrix[0].length;i++)
{
if(matrix[0][i]==0)
{
rowFlag = true;
break;
}
}
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;
}
}
}
for(int i=1;i<matrix.length;i++)
{
for(int j=1;j<matrix[0].length;j++)
{
if(matrix[i][0]==0 || matrix[0][j]==0)
matrix[i][j] = 0;
}
}
if(colFlag)
{
for(int i=0;i<matrix.length;i++)
{
matrix[i][0] = 0;
}
}
if(rowFlag)
{
for(int i=0;i<matrix[0].length;i++)
{
matrix[0][i] = 0;
}
}
}

这道题也是cc150里面比較经典的题目,看似比較简单,却能够重重优化,终于达到常量空间。事实上面试中面试官看重的是对于算法时间空间复杂度的理解,对优化的概念,这些经常比题目本身的难度更加重要,寻常做题还是要对这些算法分析多考虑哈。

Set Matrix Zeroes -- LeetCode的更多相关文章

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

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

  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 细节实现 Set Matrix Zeroes

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

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

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

  7. 55. Set Matrix Zeroes

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

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

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

随机推荐

  1. VS2010中配置C#Project不生成.vhost.exe和.pdb文件的方法

    在VS2010中编译C#工程时,在C#的Project的属性界面的Build选项卡中当Configuration : Relese 时,依然会生成扩展名为.vhost.exe和.pdb文件. 其中.p ...

  2. HDU 1054 Strategic Game(树形DP)

    Problem Description Bob enjoys playing computer games, especially strategic games, but sometimes he ...

  3. ios save image to album

    - (void)savePhotoToAlbum { ZoomScrollView *zoomScrollView = (ZoomScrollView*)[self.scrollView viewWi ...

  4. Decorator模式设计模式

    装饰者模式定义:动态地将责任附加到对象上. 若要扩展功能.装饰者提供了比继续更有弹性的替代方案. 简单定义:包装一个对象.以提供新的行为. 装饰者模式能够有效应对类爆炸问题. OO原则: 对扩展开放, ...

  5. Android EventBus源代码解析 带你深入理解EventBus

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40920453,本文出自:[张鸿洋的博客] 上一篇带大家初步了解了EventBus ...

  6. JBPM4实例教程

    JBPM语言概述:全名  Java Business Process Management  ,它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易扩展的可运行流程语言框架. 是开源 ...

  7. .NET(C#):浅谈程序集清单资源和RESX资源

    原文:.NET(C#):浅谈程序集清单资源和RESX资源   目录 程序集清单资源 RESX资源文件 使用ResourceReader和ResourceSet解析二进制资源文件 使用ResourceM ...

  8. ASP.NET Identity 身份验证和基于角色的授权

    ASP.NET Identity 身份验证和基于角色的授权 阅读目录 探索身份验证与授权 使用ASP.NET Identity 身份验证 使用角色进行授权 初始化数据,Seeding 数据库 小结 在 ...

  9. java多线程学习(两)——创建一个线程

    一个.java创建两个线程的方法 1.从java.lang.Thread派生一个新类线程类,其覆盖run()方法 2.实现Runnable接口.重载Runnable接口中的run()方法. 使用Thr ...

  10. Spring Security 入门详解(转)

    1.Spring Security介绍 Spring Security是基于spring的应用程序提供声明式安全保护的安全性框架,它提供了完整的安全性解决方案,能够在web请求级别和方法调用级别 处理 ...