Skip to main content
PreparationZone Logo
PreparationZone
Back to Coding

Coding · Medium · Pro

Set matrix zeroes if an element is zero

CodingMatrixSenior
Model answerPreview

Approach

  • Use first row/col as markers with two flags for row0/col0.
  • Second pass applies markers.

Java Solution

Java
void setZeroes(int[][] m) {
    boolean row0 = false, col0 = false;
    for (int j = 0; j < m[0].length; j++) if (m[0][j] == 0) row0 = true;…