Java O(n+m) solution


  • 0
    A
    public class Solution {
        public void setZeroes(int[][] matrix) {
            int r = matrix.length;
            int c = matrix[0].length;
            boolean[] row = new boolean[r];
            boolean[] col = new boolean[c];
            
            for(int i=0;i<r;i++){
                for(int j=0;j<c;j++){
                    int num = matrix[i][j];
                    if(num == 0){
                        row[i] = true;
                        col[j] = true;
                    }
                }
            }
            
            for(int i=0;i<r;i++){
    
                for(int j=0;j<c;j++){
                   
                    if(row[i] || col[j]){
                        matrix[i][j] = 0;
                    }
                }
            }
        }
    }
    

Log in to reply
 

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