-
Notifications
You must be signed in to change notification settings - Fork 1
/
setMatrixZero.java
74 lines (61 loc) · 2.27 KB
/
setMatrixZero.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package aryanhere.Striver;
import java.util.*;
public class setMatrixZero {
static void markRow(ArrayList<ArrayList<Integer>> matrix, int n, int m, int i) {
for (int j = 0; j < m; j++) {
if (matrix.get(i).get(j) != 0) {
matrix.get(i).set(j, -1);
}
}
}
static void markColumn(ArrayList<ArrayList<Integer>> matrix, int n, int m, int j) {
for (int i = 0; i < n; i++) {
if (matrix.get(i).get(j) != 0) {
matrix.get(i).set(j, -1);
}
}
}
static ArrayList<ArrayList<Integer>> zeroMatrix(ArrayList<ArrayList<Integer>> matrix, int n, int m) {
ArrayList<Integer> rowsToZero = new ArrayList<>();
ArrayList<Integer> colsToZero = new ArrayList<>();
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix.get(i).get(j) == 0) {
rowsToZero.add(i);
colsToZero.add(j);
}
}
}
for (int i = 0; i < rowsToZero.size(); i++) {
markRow(matrix, n, m, rowsToZero.get(i));
}
for (int j = 0; j < colsToZero.size(); j++) {
markColumn(matrix, n, m, colsToZero.get(j));
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (matrix.get(i).get(j) == -1) {
matrix.get(i).set(j, 0);
}
}
}
return matrix;
}
public static void main(String[] args) {
ArrayList<ArrayList<Integer>> matrix = new ArrayList<>();
matrix.add(new ArrayList<>(Arrays.asList(1, 1, 1)));
matrix.add(new ArrayList<>(Arrays.asList(1, 0, 1)));
matrix.add(new ArrayList<>(Arrays.asList(1, 1, 1)));
int n = matrix.size();
int m = matrix.get(0).size();
ArrayList<ArrayList<Integer>> ans = zeroMatrix(matrix, n, m);
System.out.println("The Final matrix is: ");
for (ArrayList<Integer> row : ans) {
for (Integer ele : row) {
System.out.print(ele + " ");
}
System.out.println();
}
}
}
// this question needs a revisionn.!