File size: 1,365 Bytes
c574d3a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import java.io.*;
import java.util.*;
class SetMatrixZeroes {
public void setMatrixZeroes(int[][] matrix) {
int row = matrix.length;
int col = matrix[0].length;
int[] rowZero = new int[row];
int[] colZero = new int[col];
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
if(matrix[i][j]==0) {
rowZero[i]=1;
colZero[j]=1;
}
}
}
for(int i=0;i<row;i++) {
for(int j=0;j<col;j++) {
if(rowZero[i]==1 || colZero[j] == 1) {
matrix[i][j] =0;
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[][] matrix = new int[n][m];
for(int i = 0 ; i < n ; ++i) {
for(int j = 0 ; j < m ; ++j) {
matrix[i][j] = in.nextInt();
}
}
in.close();
new SetMatrixZeroes().setMatrixZeroes(matrix);
for(int i = 0 ; i < n ; ++i) {
for(int j = 0 ; j < m ; ++j) {
System.out.print(matrix[i][j]);
System.out.print(' ');
}
System.out.println();
}
}
} |