zoukankan      html  css  js  c++  java
  • 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 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?

    Analysis:

    O(m+n) space is easy. The hard problem is O(1) space solution.

    O(m+n) space solution:

     1 public class Solution {
     2     public void setZeroes(int[][] matrix) {
     3         int xLen = matrix.length;
     4         if (xLen==0) return;
     5         int yLen = matrix[0].length;
     6         if (yLen==0) return;
     7 
     8         boolean[] rowZero = new boolean[xLen];
     9         boolean[] colZero = new boolean[yLen];
    10         Arrays.fill(rowZero,false);
    11         Arrays.fill(colZero,false);
    12 
    13         for (int i=0;i<xLen;i++)
    14             for (int j=0;j<yLen;j++)
    15                 if (matrix[i][j]==0){
    16                     rowZero[i] = true;
    17                     colZero[j] = true;
    18                 }
    19 
    20         for (int i=0;i<xLen;i++)
    21             for (int j=0;j<yLen;j++)
    22                 if (rowZero[i] || colZero[j]) 
    23                     matrix[i][j] = 0;
    24         return;
    25     }
    26 }

    O(1) space solution:

    We can use the first row and first col to store the information whether the row and the col should be turn to zero. If m[i][j]==0, then we set m[0][j]=m[i][0]=0. After scan all matrix, we then turn to zeroes according the information.

    At last, if the first row and first col have zero at the begnning, we further turn them into zeroes.

    NOTE: This very smart solution is not figured out by mysefl, so READ IT AGAIN.

     1 public class Solution {
     2     public void setZeroes(int[][] matrix) {
     3         int xLen = matrix.length;
     4         if (xLen==0) return;
     5         int yLen = matrix[0].length;
     6         if (yLen==0) return;
     7 
     8         boolean firstRowZero = false, firstColZero = false;
     9         for (int i=0;i<xLen;i++)
    10             if (matrix[i][0]==0) {
    11                 firstColZero = true;
    12                 break;
    13             }
    14 
    15         for (int i=0;i<yLen;i++)
    16             if (matrix[0][i]==0) {
    17                 firstRowZero = true;
    18                 break;
    19             } 
    20 
    21         for (int i=1;i<xLen;i++)
    22             for (int j=1;j<yLen;j++)
    23                 if (matrix[i][j]==0){
    24                     matrix[i][0] = 0;
    25                     matrix[0][j] = 0;
    26                 }
    27 
    28         for (int i=1;i<xLen;i++)
    29             for (int j=1;j<yLen;j++)
    30                 if (matrix[i][0]==0 || matrix[0][j]==0) 
    31                     matrix[i][j] = 0;
    32         if (firstRowZero)
    33             for (int i=0;i<yLen;i++) matrix[0][i]=0;
    34         if (firstColZero)
    35             for (int i=0;i<xLen;i++) matrix[i][0]=0;       
    36 
    37           
    38         return;
    39     }
    40 }
  • 相关阅读:
    多台计算机之间数据同步——1.[转]网线制作图解教程
    离心泵的使用注意事项泄露或未排气造成扬程不够
    家庭上网用路由器和ADSL的连接
    专业FLV地址解析
    [求助]带程序访问控制的防火墙 eTrust Personal Firewall 和卡巴斯基2009引起冲突造成系统频繁死机
    DV录像带导出一定要用1394
    Cursor:url()的使用
    理解并解决JavaScript内存泄漏
    CodeIgniter的HMVC
    关于在IE下JavaScript的 Stack overflow at line 错误可能的原因
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4102674.html
Copyright © 2011-2022 走看看