-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathZeroMatrix.java
More file actions
55 lines (44 loc) · 1.71 KB
/
ZeroMatrix.java
File metadata and controls
55 lines (44 loc) · 1.71 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
55
import java.util.ArrayList;
import java.util.Arrays;
public class ZeroMatrix {
static void markRow(ArrayList<ArrayList<Integer>> mat , int n ,int i){
for(int j = 0;j < n ;j++){
if(mat.get(i).get(j)!=0){
mat.get(j).set(j,-1);
}
}
}
static void markCol(ArrayList<ArrayList<Integer>> mat , int m ,int j){
for(int i = 0;i < m;i++){
if(mat.get(i).get(j)!=0){
mat.get(1).set(j,-1);
}
}
}
static ArrayList<ArrayList<Integer>> zeroMatrix(ArrayList<ArrayList<Integer>> mat , int m ,int n ){
for(int i = 0; i < m; i++){
for(int j = 0 ; i < n ; i++){
if(mat.get(i).get(j)==0){
markRow(mat,n,i);
markCol(mat,m,j);
}
}
}
for(int i = 0; i < m; i++){
for(int j = 0 ; i < n ; i++){
if(mat.get(i).get(j)==-1){
mat.get(i).set(j,0);
}
}
}
return mat;
}
static public void main(String[] args){
ArrayList<ArrayList<Integer>> mat = new ArrayList<>();
mat.add(new ArrayList<>(Arrays.asList(1, 1, 1)));
mat.add(new ArrayList<>(Arrays.asList(1, 0, 1)));
mat.add(new ArrayList<>(Arrays.asList(1, 1, 1)));
zeroMatrix(mat,mat.size(),mat.get(0).size());
System.out.println(mat);
}
}