Java Solution using sets


  • 0
    S
    class Solution {
        public void setZeroes(int[][] matrix) {
            Set<Integer> set_row = new TreeSet<Integer>();
            Set<Integer> set_col = new TreeSet<Integer>();
            
            for(int i=0;i<matrix.length;i++){
                for(int j=0;j<matrix[0].length;j++){
                    if(matrix[i][j]==0){
                        set_row.add(i);
                        set_col.add(j);
                    } 
                }
            }
            
            for(Integer col:set_col){
                for(int i=0;i<matrix.length;i++) matrix[i][col]=0;
            }
            
            for(Integer row:set_row){
                for(int i=0;i<matrix[0].length;i++) matrix[row][i]=0;
            }
        }
    }
    

Log in to reply
 

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