-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetMatrixZeroes.java
More file actions
44 lines (40 loc) · 1.12 KB
/
SetMatrixZeroes.java
File metadata and controls
44 lines (40 loc) · 1.12 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
import java.util.ArrayList;
public class SetMatrixZeroes {
public static void main(String args[]){
ArrayList<Integer> row =new ArrayList<>();
ArrayList<Integer> col =new ArrayList<>();
int arr[][]={{1,2,3},
{0,0,6}
};
int lenrow = arr.length;
int lencol = arr[0].length;
for(int i=0;i<lenrow;i++){
for(int j=0;j<lencol;j++){
if(arr[i][j]==0){
if(!row.contains(i))
row.add(i);
if(!col.contains(j))
col.add(j);
}
}
}
for(int i=0; i<row.size(); i++){
int val = row.get(i);
for(int j=0; j< lencol;j++){
arr[val][j]=0;
}
}
for(int i=0; i<col.size(); i++){
int val = col.get(i);
for(int j=0; j< lenrow;j++){
arr[j][val]=0;
}
}
for(int i=0;i<lenrow;i++){
for(int j=0;j<lencol;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}