-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSet Matrix Zero.java
More file actions
31 lines (24 loc) · 924 Bytes
/
Set Matrix Zero.java
File metadata and controls
31 lines (24 loc) · 924 Bytes
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
//https://www.interviewbit.com/problems/set-matrix-zeros/
public class Solution {
public void setZeroes(ArrayList<ArrayList<Integer>> a) {
HashSet<Integer> rows = new HashSet<>();
HashSet<Integer> cols = new HashSet<>();
for(int r = 0; r < a.size(); r++){
for(int c = 0; c < a.get(0).size(); c++){
int num = a.get(r).get(c);
if(num == 0){
if(!rows.contains(r))
rows.add(r);
if(!cols.contains(c))
cols.add(c);
}
}
}
for(int r = 0; r < a.size(); r++){
for(int c = 0; c < a.get(0).size(); c++){
if(rows.contains(r) || cols.contains(c))
a.get(r).set(c, 0);
}
}
}
}