73 Set Matrix Zeroes(Java最优版本与普通版本)

题目链接

题目描述

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

Example 1:

Input: 
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
Output: 
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Example 2:

Input: 
[
  [0,1,2,0],
  [3,4,5,2],
  [1,3,1,5]
]
Output: 
[
  [0,0,0,0],
  [0,4,5,0],
  [0,3,1,0]
]

Follow up

  • 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?

思路

第一种思路(O(mn)space)

题目说了,最差的算法是使用O(mn)的·空间,那么我们可以用一个和matrix数组同样大的boolean型数组来记录是否为0。

class Solution {
    public void setZeroes(int[][] matrix) {
        int rows=matrix.length;
        int columns=matrix[0].length;
        if(rows==0&&columns==0)
            return ;
        boolean [][]zeros=new boolean[rows][columns];
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<columns;j++)
            {
                if(matrix[i][j]==0)
                {
                    zeros[i][j]=true;
                 }
            }
        }
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<columns;j++)
            {
                if(zeros[i][j]==false)
                    continue;
                if(zeros[i][j]==true)
                {
                    //列变成0
                    for(int k=0;k<rows;k++)
                    {
                        matrix[k][j]=0;
                    }
                    //行变成0
                    for(int t=0;t<columns;t++)
                    {
                        matrix[i][t]=0;
                    }
                }
                
            }
        }
      
    }
}

第二种思路(O(C)space)

使用两个flag去判断首行和首列是否为0
如果matrix[i][j]==0;
那么就把他第一行第j列(最上面)置为0,(第一列第i行)最左边置为0
第二次判断有两种思路:
1.遍历整个数组,matrix[i][j],j列的第一行或i行第一列是否为0,就可以判断,是否需要变为0。
2.另一种思路是,直接判断变换后的第一行和第一列,比如第一行中哪个值为0,那么就把该列都变为0,如果第一列中哪个值为0,那么就把该行都变为0。
最后判断两个flag是否为true,为true的话就把第一行/第一列变为0。

class Solution {
    public void setZeroes(int[][] matrix) {
        boolean row0_flag=false;
        boolean column0_flag=false;
        int rows=matrix.length;
        int cols=matrix[0].length;
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<cols;j++)
            {
                if(matrix[i][j]==0)
                {
                    if(i==0&&row0_flag==false)
                        row0_flag=true;
                    if(j==0&&column0_flag==false)
                        column0_flag=true;
                    //第一行第j列(最上面)为0
                    matrix[0][j]=0;
                    //第一列第i行(最左边)为0
                    matrix[i][0]=0;
                }
            }
        }
        for(int i=1;i<rows;i++)
        {
            for(int j=1;j<cols;j++)
            {
                if(matrix[0][j]==0||matrix[i][0]==0)
                    matrix[i][j]=0;
            }
        }
        if(row0_flag==true)
        {
            for(int j=0;j<cols;j++)
            {
                matrix[0][j]=0;
            }
        }
        if(column0_flag==true)
        {
            for(int i=0;i<rows;i++)
            {
                matrix[i][0]=0;
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章