Skip to content

Instantly share code, notes, and snippets.

@hilda8519
Created June 18, 2014 23:30
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 hilda8519/2d76dc3a2bb10751c167 to your computer and use it in GitHub Desktop.
Save hilda8519/2d76dc3a2bb10751c167 to your computer and use it in GitHub Desktop.
package setZero;
public class setZero {
public static int[][] setZeros(int[] [] matrix){
boolean[] row=new boolean[matrix.length];
boolean[] column=new boolean[matrix[0].length];
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==0){
row[i]=true;
row[j]=true;
}
}
}
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(row[i]||column[j]){
matrix[i][j]=0;
}
}
}
return matrix;
}
public static void main(String[] args){
int[][] matrix={{1,0,3},{1,1,1},{2,5,7}};
matrix=setZeros(matrix);
for(int i = 0; i < matrix.length; i++)
{
System.out.println();
for(int j = 0; j < matrix[0].length; j++)
{
System.out.print(matrix[i][j]+",");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment