• 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 }
  • 相关阅读:
    构造函数与析构函数2
    构造函数与析构函数(其中有两点值得学习)
    构造函数含有含默认值的参数
    A simple stack
    指针与const
    构造函数与析构函数
    构造函数的创建
    类的定义
    程序的堆与栈(转载)
    OpenStack 安装:glance 安装
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4102674.html
Copyright © 2020-2023  润新知