Bootstrap

【hot100】073矩阵置零

一、思路

此题要求原地置0,但我看了评论区很多人说没有必要,现在普遍时间优先于空间,所以采用了O(m+n)的空间复杂度做法

二、记忆

1.标记矩阵的思路

2.

int y = matrix.length;
int x = matrix[0].length;

三、代码

public void setZeroes(int[][] matrix){
        int y = matrix.length;
        int x = matrix[0].length;
        boolean[] row = new boolean[y];
        boolean[] col = new boolean[x];
        for(int i =0;i<y;i++){
            for(int j= 0;j<x;j++){
               if(matrix[i][j]==0){
                   for (int a=0;a<x;a++) {
                       row[i]=true;
                       col[j]=true;
                   }
               }
            }
        }
        for(int i =0;i<y;i++){
            for(int j= 0;j<x;j++){
                if(row[i] || col[j]){
                    matrix[i][j]=0;
                }
            }
        }
    }

;