-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetZeroRowAndColumn.java
More file actions
78 lines (74 loc) · 2.32 KB
/
Copy pathSetZeroRowAndColumn.java
File metadata and controls
78 lines (74 loc) · 2.32 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
*
*/
import java.util.*;
public class SetZeroRowAndColumn{
public static void setZeroes(ArrayList<ArrayList<Integer>> A) {
int[] rows = new int[A.size()];
int[] columns = new int[A.get(0).size()];
for(int i = 0; i < A.size(); i++){
for(int j = 0; j < A.get(i).size(); j++){
if(A.get(i).get(j) == 0){
rows[i] = 1;
columns[j] = 1;
}
}
}
for(int i = 0; i < A.size(); i++){
for(int j = 0; j < A.get(i).size(); j++){
if(rows[i] == 1 ||columns[j] == 1){
A.get(i).set(j, 0);
}
}
}
for(int i = 0; i < A.size(); i++) {
System.out.print("[");
for(int j = 0; j < A.get(i).size(); j++){
System.out.print(A.get(i).get(j));
if(j != A.get(i).size()-1)
System.out.print(", ");
}
if(i != A.size()-1)
System.out.print("] \n ");
else
System.out.print("]");
}
}
public static void main(String[] args){
ArrayList<ArrayList<Integer>> A = new ArrayList<ArrayList<Integer>>();
A = Spiral.generateMatrix(3);
// for(int k = 0; k < A.size(); k++){
// A.add(new ArrayList<Integer>());
// for(int j = 0; j < A.get(k).size(); j++){
// A.get(k).add(0);
// }
// }
// System.out.println("printing");
// for(int i = 0; i < A.size(); i++) {
//
// System.out.print("[");
// for(int j = 0; j < A.get(i).size(); j++){
// System.out.print(A.get(i).get(j));
// if(j != A.get(i).size()-1)
// System.out.print(", ");
// }
// if(i != A.size()-1)
// System.out.print("], ");
// else
// System.out.print("]");
//
// }
// A.get(0).set(0,1);
// A.get(0).set(1,0);
// A.get(0).set(2,1);
//
// A.get(1).set(0,1);
// A.get(1).set(1,1);
// A.get(1).set(2,1);
//
// A.get(2).set(0,1);
// A.get(2).set(1,1);
// A.get(2).set(2,1);
setZeroes(A);
}
}