Skip to content

Instantly share code, notes, and snippets.

@iwilbert
Created June 18, 2014 00:20
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 iwilbert/9a6463330ddf8ac34b61 to your computer and use it in GitHub Desktop.
Save iwilbert/9a6463330ddf8ac34b61 to your computer and use it in GitHub Desktop.
public void zero(int[][] matrix) {
if(matrix == null)
return;
int m = matrix.length;
if(m == 0)
return;
int n = matrix[0].length;
boolean[] row_zero = new boolean[m];
boolean[] col_zero = new boolean[n];
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
{
if(matrix[i][j] == 0)
{
row_zero[i] = true;
col_zero[j] = true;
}
}
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++)
{
if(row_zero[i] || col_zero[j])
matrix[i][j] = 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment