-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathZeroMatrix.java
More file actions
35 lines (30 loc) · 816 Bytes
/
ZeroMatrix.java
File metadata and controls
35 lines (30 loc) · 816 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
32
33
34
35
/*https://binarysearch.com/problems/Zero-Matrix*/
import java.util.*;
class Solution {
public int[][] solve(int[][] matrix) {
HashSet<Integer> rows = new HashSet<Integer>(), cols = new HashSet<Integer>();
int i, j, m = matrix.length, n = matrix[0].length;
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
if (matrix[i][j] == 0)
{
rows.add(i);
cols.add(j);
}
}
}
for (i = 0; i < m; ++i)
{
for (j = 0; j < n; ++j)
{
if (rows.contains(i) || cols.contains(j))
{
matrix[i][j] = 0;
}
}
}
return matrix;
}
}