-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetMatrixZeroes.java
More file actions
47 lines (47 loc) · 1.21 KB
/
SetMatrixZeroes.java
File metadata and controls
47 lines (47 loc) · 1.21 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
public class SetMatrixZeroes {
public void setZeroes(int[][] ma) {
int m = ma.length;
int n = ma[0].length;
int flagR = 0, flagC = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
if(ma[i][j] == 0){
if(i == 0){
flagR = 1;
}else{
ma[0][j] = 0;
}
if(j == 0){
flagC = 1;
}else{
ma[i][0] = 0;
}
}
}
}
for(int i = 1; i < m; i++){
if(ma[i][0] == 0){
for(int j = 1; j < n; j++){
ma[i][j] = 0;
}
}
}
for(int i = 1; i < n; i++){
if(ma[0][i] == 0){
for(int j = 1; j < m; j++){
ma[j][i] = 0;
}
}
}
if(flagC == 1){
for(int i = 0; i < m; i++){
ma[i][0] = 0;
}
}
if(flagR == 1){
for(int i = 0; i < n; i++){
ma[0][i] = 0;
}
}
}
}