Skip to content

Instantly share code, notes, and snippets.

@asdw3276
Created June 18, 2014 17:23
Show Gist options
  • Select an option

  • Save asdw3276/fda81d69ab0bc1b0bb3b to your computer and use it in GitHub Desktop.

Select an option

Save asdw3276/fda81d69ab0bc1b0bb3b to your computer and use it in GitHub Desktop.
1.7
public class matrixsetzero{
public static void main(String[] args)
{
int[][] matrix = {{3,5,6,78,8},{1,2,4,5,6},{0,1,2,0,8},{0,0,1,1,1}};
matrix = matrixsetzero(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]+",");
}
}
}
public static int[][] matrixsetzero(int[][] matrix)
{
if(matrix.length == 0)
return matrix;
int m = matrix.length;
int n = matrix[0].length;
int[] mflag = new int[m];
int[] nflag = new int[n];
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(matrix[i][j] == 0)
{
mflag[i] = 1;
nflag[j] = 1;
}
}
}
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(mflag[i] == 1 || nflag[j] == 1)
matrix[i][j] = 0;
}
}
return matrix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment