Skip to content

Instantly share code, notes, and snippets.

@berkayk
Created January 28, 2016 22:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save berkayk/f1a0cefc3a580440b1f0 to your computer and use it in GitHub Desktop.
Save berkayk/f1a0cefc3a580440b1f0 to your computer and use it in GitHub Desktop.
public void fillZeros(int[][] matrix) {
int numRows = matrix.length;
int numCols = matrix[0].length;
boolean[] rows = new boolean[numRows];
boolean[] cols = new boolean[numCols];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (matrix[i][j] == 0) {
rows[i] = true;
cols[j] = true;
}
}
}
// seconds pass fill if it's empty
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (rows[i] || cols[j]) {
matrix[i][j] = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment