Using a POJO to represent points to be set to 0s


  • 0
    M
    public class Solution {
        public void setZeroes(int[][] matrix) {
            
            List<Marker> markers = new ArrayList<>();
            
            // First pass identify markers
            for(int i=0;i<matrix.length;i++){
                for(int j=0;j<matrix[i].length;j++){
                    if(matrix[i][j] == 0){
                        Marker marker = new Marker(i, j);
                        markers.add(marker);
                    }
                }
            }
            
            // Second pass, set corresponding values to 0s
            for(Marker marker: markers){
                setMat(marker, matrix);
            }
        }
        
        private void setMat(Marker marker, int[][] matrix){
            int row = marker.getRow();
            int col = marker.getCol();
            
            // set row
            for(int i=0;i<matrix[0].length;i++){
                matrix[row][i] = 0;
            }
            
            // set col
            for(int i=0;i<matrix.length;i++){
                matrix[i][col] = 0;
            }
        }
    }
    
    class Marker{
        private int row;
        private int col;
        
        public Marker(int row, int col){
            this.row = row;
            this.col = col;
        }
        
        public int getRow(){
            return row;
        }
        
        public int getCol(){
            return col;
        }
    }
    

Log in to reply
 

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