Last active
August 29, 2015 14:02
-
-
Save k1xme/849a31e739c234a7555e 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
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