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;
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章