Preview:
class Solution {
    public void setZeroes(int[][] matrix) {
        int rowsL = matrix.length;
        int colL = matrix[0].length;

        int[] r = new int[rowsL];
        int[] c = new int[colL];

        boolean firstRowZero = false;
        boolean firstColZero = false;

        for(int i=0;i<colL;i++) {// check if first row needs to zeroed 
            if(matrix[0][i] == 0) {
                firstRowZero = true;
                break;
            }
        }

        for(int i=0;i<rowsL;i++) {// check if first col needs to be zeroed 
            if(matrix[i][0] == 0) {
                firstColZero = true;
                break;
            }
        }

        // use first row/col as markers
        for(int i=1;i<rowsL;i++){
            for(int j=1;j<colL;j++) {
                if(matrix[i][j] == 0) {
                    matrix[i][0] = 0; // mark row
                    matrix[0][j] = 0; // mark col
                }
            }
        }

        // apply markers (skipping first row and col) 
        for(int i=1;i<rowsL;i++) {
            if(matrix[i][0] == 0) {
                for(int j=1;j<colL;j++) {
                    matrix[i][j] =0;
                }
            }
        }

        for(int j =1;j<colL;j++) {
            if(matrix[0][j] == 0) {
                for(int i=1;i<rowL;i++) {
                    matrix[i][j] = 0;
                }
            }
        }

        // now handling first row and col
        if (firstRowZero) {
            for (int j = 0; j < colL; j++) {
                matrix[0][j] = 0;
            }
        }

        if (firstColZero) {
            for (int i = 0; i < rowsL; i++) {
                matrix[i][0] = 0;
            }
        }
        
    }
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter