Created
January 28, 2016 22:13
-
-
Save berkayk/f1a0cefc3a580440b1f0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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