Untitled

 avatar
unknown
java
a year ago
1.0 kB
5
Indexable
class Solution {

        class Pair {
        int row;
        int col;
      
        
        public Pair( int row,int col) {
            this.row = row;
            this.col = col;
            
        }
    }
    public void setZeroes(int[][] matrix) {
      
        // O(MxN)+M+N  : Space -> O(n)
        ArrayList<Pair> list  = new ArrayList<>();
        for (int i = 0; i <matrix.length ; i++) {
            for (int j = 0; j <matrix[0].length ; j++) {
                if(matrix[i][j]==0){
                    // add the i,j pair
                    list.add(new Pair(i,j));
                }
            }
        }
        
        for(Pair p : list){
            markZero(matrix,p.row,p.col);
        }
            
    }

       
    public static void markZero(int[][] matrix, int row, int col){
         for (int i = 0; i <matrix.length; i++) { // row----->0
          matrix[i][col]=0;

    }

    for (int j = 0; j <matrix[0].length ; j++) { // col-----0
      matrix[row][j]=0;
    }
    
    }
}
    
    
Editor is loading...
Leave a Comment