73. Set Matrix Zeroes - Medium

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?

把第一行和第一列的元素当作flag,表示这一行/列有无0

首先扫描matrix,如果有元素为0,就把该元素所在的行、列的第一个元素设为0,作为稍后整体设零的flag

注意第一行和第一列的处理!!如果第一行/列有0,用另外的flag表示,最后再处理这两部分。如果一开始就设0的话,整个矩阵都会变成0

扫描第一行,如果有0,就把当前列全部设为0;扫描第一列,如果有0,就把当前行全部设为0

最后处理第一行/列,如果有0的话

time: O(M * N), space: O(1)

class Solution {
    public void setZeroes(int[][] matrix) {
        if(matrix == null || matrix.length == 0) return;
        int m = matrix.length, n = matrix[0].length;
        boolean rowHasZero = false, colHasZero = false;
        
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(matrix[0][j] == 0) rowHasZero = true;
                if(matrix[i][0] == 0) colHasZero = true;
            }
        }
        
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }
        
        for(int i = 1; i < m; i++) {
            if(matrix[i][0] == 0) {
                for(int j = 1; j < n; j++)
                    matrix[i][j] = 0;
            }
        }
        for(int j = 1; j < n; j++) {
            if(matrix[0][j] == 0) {
                for(int i = 1; i < m; i++)
                    matrix[i][j] = 0;
            }
        }
        
        if(colHasZero) {
            for(int i = 0; i < m; i++)
                matrix[i][0] = 0;
        }
        if(rowHasZero) {
            for(int j = 0; j < n; j++)
                matrix[0][j] = 0;
        }
    }
}

改进:少一个pass

class Solution {
    public void setZeroes(int[][] matrix) {
        if(matrix == null || matrix.length == 0) return;
        int m = matrix.length, n = matrix[0].length;
        boolean rowHasZero = false, colHasZero = false;
        
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                    if(j == 0) colHasZero = true;
                    if(i == 0) rowHasZero = true;
                }
            }
        }
        
        for(int i = 1; i < m; i++) {
            if(matrix[i][0] == 0) {
                for(int j = 1; j < n; j++)
                    matrix[i][j] = 0;
            }
        }
        for(int j = 1; j < n; j++) {
            if(matrix[0][j] == 0) {
                for(int i = 1; i < m; i++)
                    matrix[i][j] = 0;
            }
        }
        
        if(colHasZero) {
            for(int i = 0; i < m; i++)
                matrix[i][0] = 0;
        }
        if(rowHasZero) {
            for(int j = 0; j < n; j++)
                matrix[0][j] = 0;
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/fatttcat/p/10090237.html