-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet_matrix_zeroes.java
42 lines (41 loc) · 1.2 KB
/
Set_matrix_zeroes.java
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
import java.util.Arrays;
public class Set_matrix_zeroes {
public static void main(String[] args) {
int[][] matrix = {{1,1,1},{1,0,1},{1,1,1}};
int[] row = new int[matrix.length];
int[] column = new int[matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
if(matrix[i][j] == 0)
{
row[i] = 1;
column[j] = 1;
}
}
}
set_zero(matrix,row,column);
for (int i = 0; i < matrix.length; i++) {
System.out.println(Arrays.toString(matrix[i]));
}
}
static void set_zero(int[][] matrix, int[] row, int[] column)
{
for (int i = 0; i < row.length; i++) {
if(row[i]==1)
{
for (int j = 0; j < column.length; j++) {
matrix[i][j]=0;
}
}
else
{
for (int j = 0; j < column.length; j++) {
if(column[j]==1)
{
matrix[i][j]=0;
}
}
}
}
}
}