2ms Java solution


  • 1
    B

    public class Solution {
    public void setZeroes(int[][] matrix) {
    int m = matrix.length;
    int n = matrix[0].length;
    boolean [] row = new boolean[m];
    boolean [] column = new boolean[n];

        for (int i = 0; i < m; i++) {
            for (int j = 0; j<n; j++) {
                if (matrix[i][j] == 0) {
                    row [i] = true;
                    column [j] =true;
                }
            }
        }
        
        for (int k = 0 ; k < m ; k ++) {
            if (row[k] == true) {
                for (int g = 0; g < n; g ++) {
                    matrix[k][g] = 0;
                }
                row[k] = false;
            }
        }
        
        for (int k = 0; k < n; k++) {
            if (column[k] == true) {
                 for (int g = 0; g < m; g ++) {
                    matrix[g][k] = 0;
                }
                
                column[k] = false;
            }
        }
        
        
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.