This repository was archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZeroMatrix.java
More file actions
53 lines (46 loc) · 1.29 KB
/
ZeroMatrix.java
File metadata and controls
53 lines (46 loc) · 1.29 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
package com.ematrix;
import java.util.Arrays;
public class ZeroMatrix {
public void transForm(int[][] S){
int n=S.length;
System.out.print(n);
boolean[] rows=new boolean[S.length];
boolean[] columns=new boolean[S[0].length];
Arrays.fill(rows,false);
Arrays.fill(columns,false);
for(int i=0;i<n;i++){
for(int j=0;j<S[i].length;j++){
if(S[i][j]==0){
rows[i] =true;
columns[j]=true;
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<S[i].length;j++){
if(rows[i]){
S[i][j]=0;
}
if( columns[j]){
S[i][j]=0;
}
}
}
}
public static void main(String[] args) {
int[][] a = {
{0, 2, 3, 4,5},
{5, 6, 7, 0,4},
{9,10,11,12,6},
{13,7,15,16,7}
};
ZeroMatrix zeroMatrix =new ZeroMatrix();
zeroMatrix.transForm(a);
for (int i = 0; i < a.length; ++i) {
for(int j = 0; j < a[i].length; ++j) {
System.out.print(a[i][j]+"\t");
}
System.out.print("\n");
}
}
}