Skip to content

Instantly share code, notes, and snippets.

@k1xme
Last active August 29, 2015 14:02
Show Gist options
  • Save k1xme/849a31e739c234a7555e to your computer and use it in GitHub Desktop.
Save k1xme/849a31e739c234a7555e to your computer and use it in GitHub Desktop.
class Solution{
/*
* Space: O(n); Time(n^2)
*/
public static void setZero(int[][] matrix){
if(matrix == null) return;
int row_num = martix.length;
int col_num = martix[0].length;
boolean[] row = new boolean[row_num];
boolean[] col = new boolean[col_num];
for(int i = 0; i < row_num; i++) row[i] = false;
for(int i = 0; i < col_num; i++) col[i] = false;
for(int i = 0; i < row_num; i++)
for(int j=0; j< col_num; j++){
if(matrix[i][j] == 0){
row[i] = true;
col[j] = true;
}
}
for(int i = 0; i < row_num; i++){
if(row[i]){
for(int j = 0; j < col_num; j++) matrix[i][j] = 0;
}
}
for(int i = 0; i < col_num; i++){
if(col[i]){
for(int j = 0; j < row_num; j++) matrix[j][i] = 0;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment