-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathSetMatrix0.java
55 lines (46 loc) · 1.28 KB
/
SetMatrix0.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
package Array;
import java.util.ArrayList;
import java.util.Arrays;
/**
* Author - archit.s
* Date - 04/09/18
* Time - 1:36 PM
*/
public class SetMatrix0 {
public void setZeroes(ArrayList<ArrayList<Integer>> a) {
int rowSize = a.size();
int colSize = a.get(0).size();
int[] rows = new int[rowSize];
Arrays.fill(rows, 0);
int[] cols = new int[colSize];
Arrays.fill(cols, 0);
int rowZero = 0, colZero = 0;
for(int i=0;i<rowSize;i++){
for(int j=0;j<colSize;j++){
if(a.get(i).get(j) == 0 ){
if(i == 0){
rowZero = 1;
}
if(j == 0){
colZero = 1;
}
rows[i] = 1;
cols[j] = 1;
}
}
}
for(int i=0;i<rowSize;i++){
for(int j=0;j<colSize;j++){
if(rows[i] == 1 || cols[j] == 1){
a.get(i).set(j, 0);
}
if(i == 0 && rowZero == 1){
a.get(0).set(j, 0);
}
if( j == 0 && colZero == 1){
a.get(i).set(0, 0);
}
}
}
}
}