publicclassSolution{ publicvoidsetZeroes(int[][] matrix){ int col0 = 1; for (int i = 0; i < matrix.length; i++) { if (matrix[i][0] == 0) { col0 = 0; } for (int j = 1; j < matrix[0].length; j++) { if (matrix[i][j] == 0) { matrix[i][0] = 0; matrix[0][j] = 0; } } } // start from right bottom corner // if start from left upper corner, must start from (1,1) // the first column should be set only after setting // the rest matrix for (int i = matrix.length - 1; i >= 0; i--) { for (int j = matrix[0].length - 1; j >= 1; j--) { if (matrix[i][0] == 0 || matrix[0][j] == 0) { matrix[i][j] = 0; } } if (col0 == 0) { matrix[i][0] = 0; } } } }