[LeetCode] 73. Set Matrix Zeroes

题:https://leetcode.com/problems/set-matrix-zeroes/description/

题目

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?

思路

题目大意

给定一个矩阵,若元素 martix[i][j] == 0 ,则matrix 中 row == i 或 col == j 的元素置为0。

思路

方法一 space O(m+n)

  1. 设置两个数组 rarray,carray。若数组rarray[i] == 1,那么matrix的第i行均设置为0。若carray[j] == 1,那么matrix的第j列均设置为0。
  2. 遍历整个matrix 若,元素matrix[i][j] == 0,那么rarray[i] = 0 ,carray[j] =0。
  3. 遍历matrix 若 rarray[i] == 1 或 carray[j] == 1 ,那么matrix[i][j] = 0。

方法二 space O(1)

利用 matrix第一行和第一列起到 方法一中 rarray,carray 的功能,但需要先利用两个 boolean型 is_first_row_zero ,is_is_first_col_zero 记录 matrix第一行或第一列 是否需要 置为 0 。

code

方法一 space O(m+n)

class Solution {
    public void setZeroes(int[][] matrix) {
        
        int rlen = matrix.length;
        int clen = matrix[0].length;
        int rarray[] = new int[rlen];
        int carray[] = new int[clen];        
            
        for(int i=0;i<rlen;i++)
            for(int j=0;j<clen;j++){
                if(matrix[i][j] == 0){
                    rarray[i] = 1;
                    carray[j] = 1;
                }
            
            }
        for(int i = 0;i<rlen ;i++)
            for(int j = 0 ; j<clen;j++)
                if(rarray[i] == 1 || carray[j] == 1){
                    matrix[i][j] = 0;
                }
        
    }
}

方法二 space O(1)

class Solution {
    public void setZeroes(int[][] matrix) {
        
        int rlen = matrix.length;
        int clen = matrix[0].length;
        // int rarray[] = new int[rlen];
        // int carray[] = new int[clen];
        
        boolean is_first_row_zero = false;
        boolean is_is_first_col_zero = false;
        
        for(int j = 0 ;j<clen ;j++)
            if(matrix[0][j] == 0){
                is_first_row_zero = true;
                break;
            }
        
        for(int i=0;i<rlen;i++)
            if(matrix[i][0] == 0){
                is_is_first_col_zero = true;
                break;
            }

        for(int i=1;i<rlen;i++)
            for(int j=1;j<clen;j++){
                if(matrix[i][j] == 0){
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        for(int i = 1;i<rlen ;i++)
            for(int j = 1; j<clen;j++)
                if(matrix[i][0] == 0 || matrix[0][j] == 0){
                    matrix[i][j] = 0;
                }
        
        if(is_first_row_zero)
            for(int j = 0 ;j<clen ;j++)
                matrix[0][j] = 0;
        
        if(is_is_first_col_zero){
            for(int i=0;i<rlen;i++)
                matrix[i][0] = 0;
        }
        
    }
}

猜你喜欢

转载自blog.csdn.net/u013383813/article/details/82861984