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?

 
  1. public class Solution {
  2. public void setZeroes(int[][] matrix) {
  3. if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
  4. int row = 0, col = 0;
  5. boolean sig = true;
  6. for(int i = 0; i < matrix.length; i ++){
  7. for(int j = 0; j < matrix[0].length; j ++){
  8. if(matrix[i][j] == 0){
  9. if(sig){
  10. row = i;
  11. col = j;
  12. sig = false;
  13. } else {
  14. matrix[row][j] = 0;
  15. matrix[i][col] = 0;
  16. }
  17. }
  18. }
  19. }
  20. if(sig) return;
  21. for(int i = 0; i < matrix.length; i ++){
  22. for(int j = 0; j < matrix[0].length; j ++){
  23. if((matrix[row][j] == 0 || matrix[i][col] == 0) && i != row && j != col) matrix[i][j] = 0;
  24. }
  25. }
  26. for(int i = 0; i < matrix.length; i ++){
  27. matrix[i][col] = 0;
  28. }
  29. for(int j = 0; j < matrix[0].length; j ++){
  30. matrix[row][j] = 0;
  31. }
  32. }
  33. }

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. 在mac系统安装Apache Tomcat的详细步骤[转]

    对于Apache Tomcat 估计很多童鞋都会,那么今天就简单说下在mac上进行tomcat的安装: 第一步:下载Tomcat   这里Himi下载的tomcat version:7.0.27 直接 ...

  2. Unity3d Shortcuts

    参考:http://www.ceeger.com/Manual/ 场景视图导航  Click-drag to drag the camera around. 点击拖拽平移场景视图 Hold Alt a ...

  3. vs2010运行C程序时,运行结果窗口一闪而过

    摘要:vs2010运行C程序时,运行结果窗口一闪而过; ------------------------------------------------------------ Ctrl F5测试运行 ...

  4. 从JetBrains公司产品给我的商业模式启示

    JetBrains是捷克一家公司,专门从事IDE工具的开发,运营的产品有十几个.我因为使用JavaScript IDE工具而了解了WebStorm.进而了解了开发WebStorm的公司JetBrian ...

  5. UIScrowView swift

    // // ViewController.swift // UILabelTest // // Created by mac on 15/6/23. // Copyright (c) 2015年 fa ...

  6. Linux Shel高级技巧(目录)

    Linux Shell高级技巧(一) http://www.cnblogs.com/stephen-liu74/archive/2011/12/22/2271167.html一.将输入信息转换为大写字 ...

  7. java rest框架jersey数组单记录问题解决

    JAVA数据接口采用jersey技术,可以返回xml,json等格式,可以根据客户端请求accept,如:Application/json,Application/xml 来得到不同的接口数据,非常好 ...

  8. React + Reflux

    React + Reflux 渲染性能优化原理   作者:ManfredHu 链接:http://www.manfredhu.com/2016/11/08/23-reactRenderingPrinc ...

  9. 遗传学详解及Matlab算法实现

    遗传学算法概述 从之前转载的博客<非常好的理解遗传算法的例子>中可以知道,遗传学算法主要有6个步骤: 1. 个体编码 2. 初始群体 3. 适应度计算 4. 选择运算 5. 交叉运算 6. ...

  10. MVC中使用SignalR打造酷炫实用的即时通讯功能附源码

    前言,现在这世道写篇帖子没个前言真不好意思发出来.本贴的主要内容来自于本人在之前项目中所开发的一个小功能,用于OA中的即时通讯.由于当时走的太急,忘记把代码拿出来.想想这已经是大半年前的事情了,时间过 ...