-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
54 lines (46 loc) · 1.49 KB
/
Matrix.java
File metadata and controls
54 lines (46 loc) · 1.49 KB
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
//algorithm such that if an element in an MxN matrix is 0, its entire row and
//column is set to 0
public class Matrix {
public int[][] setToZero(int[][] matrix) {
//recreate row & column for matrix with same length
int row[] = new int[matrix.length];
int column[] = new int[matrix[0].length];
//place 1 in row and column at same length where you find 0 in matrix
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
row[i] = 1;
column[j] = 1;
}
}
}
//In matrix replace entire row and column if we find 1 at row and column length
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (row[i] == 1 || column[j] == 1) {
matrix[i][j] = 0;
}
}
}
return matrix;
}
public static void main(String[] arg) {
Matrix m = new Matrix();
int[][] matrixToTest = { { 0, 1, 1, 1 }, { 1, 1, 1, 1 }, { 1, 1, 1, 0 } };
System.out.println("Before");
for (int i = 0; i < matrixToTest.length; i++) {
for (int j = 0; j < matrixToTest[0].length; j++) {
System.out.print(matrixToTest[i][j]);
}
System.out.println(" ");
}
matrixToTest = m.setToZero(matrixToTest);
System.out.println("After");
for (int i = 0; i < matrixToTest.length; i++) {
for (int j = 0; j < matrixToTest[0].length; j++) {
System.out.print(matrixToTest[i][j]);
}
System.out.println(" ");
}
}
}