LeetCode Matrix Zeroing (73 questions)

LeetCode Matrix Zeroing

@author:Jingdai
@date:2020.09.30

Topic description (73 questions)

Given an m x n matrix, if an element is 0, set all elements in its row and column to 0. Please use the in-place algorithm.

  • Example input:
[
  [1,1,1],
  [1,0,1],
  [1,1,1]
]
  • Sample output:
[
  [1,0,1],
  [0,0,0],
  [1,0,1]
]

Ideas

See topic first thought would be to use two sets of rows which are recorded and which columns should be set to 0, when traversed matrix[i][j], if it is equal to 0, then added to the collection line i, adding sets of columns j, namely:

if (matrix[i][j] == 0) {
     
     
    rowSet.add(i);
    columnSet.add(j);
}

Then traverse the array again, and set the elements in the corresponding rows and columns to 0, that is:

if (rowSet.contains(i) || columnSet.contains(j)) {
     
     
	matrix[i][j] = 0;
}

This method is relatively intuitive and easy to understand, but it requires an extra (m+n)size of space. Here is another method that uses only a constant size of space.

According to the above method, we need to record which row and which column needs to be set to 0. We can use the elements in the 0th row and 0th column of the matrix to record, as shown in the following figure:

Insert picture description here

When matrix[1][1]equal to 0, it will be the first element of the corresponding row matrix[1][0]and the first element of the corresponding column matrix[0][1]is set to 0, the first element will be noted here that the corresponding row and column set to 0 after the judgment does not affect, because you will I found ergodic matrix, matrix[i][j]forever matrix[0][j]and matrix[i][0]judgment after the back, so it will not affect. After traversing the entire matrix, traverse the matrix again, set all the columns corresponding to 0 elements in row 0 to 0, and set all rows corresponding to 0 elements in column 0 to 0.

It seems like this is over, but there is actually a problem, look at the picture below.

Insert picture description here

When the element is located in the 0th row 0 column 0, or, as shown in matrix[1][0]and matrix[0][1]will cause matrix[0][0]becomes 0, determination is not 0 is the first element row 0 0 0 elements, so we need extra variable records Whether there are 0 elements in row 0 and column 0.

Here we set up a multi- booleanvariable isRow, record whether the zero line 0 elements, traversing the 0th row, we do not modify matrixthe value of 0 if the first row contains 0, it will be isRowset to truethe other before, but if the last matrix[0][0]value is 0, it is certainly the first column contains 0 0 (0 or which itself is, of itself is 0), then the second pass when the first modification does not modify the 0th row and 0th column, according to the following isRowand matrix[0][0]the Value to update row 0 and column 0 separately. code show as below.

Code

public void setZeroes(int[][] matrix) {
     
     
        
    if (matrix.length == 0) {
     
     
        return;
    }

    boolean isRow = false;
    int row = matrix.length;
    int column = matrix[0].length;

    for (int i = 0; i < row; i++) {
     
     
        for (int j = 0; j < column; j++) {
     
     
            if (i == 0) {
     
     
                if (matrix[i][j] == 0) {
     
     
                    isRow = true;
                }
            } else {
     
     
                if (matrix[i][j] == 0) {
     
     
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
    }

    for (int i = 1; i < row; i++) {
     
     
        for (int j = 1; j < column; j++) {
     
     
            if (matrix[i][0] == 0 || matrix[0][j] == 0) {
     
     
                matrix[i][j] = 0;
            }
        }
    }

    if (matrix[0][0] == 0) {
     
     
        for (int i = 1; i < row; i++) {
     
     
            matrix[i][0] = 0;
        }
    }

    if (isRow) {
     
     
        for (int j = 0; j < column; j++) {
     
     
            matrix[0][j] = 0;
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_41512783/article/details/108880824