forked from algorithm024/algorithm024
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetZeroes.java
More file actions
55 lines (48 loc) · 1.6 KB
/
SetZeroes.java
File metadata and controls
55 lines (48 loc) · 1.6 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
import java.util.Arrays;
/**
* @author JackWu
* @version 1.0
*/
public class SetZeroes {
public static void main(String[] args) {
Solution solution = new Solution();
int[][] arr = {{1,1,1},{1,0,1},{1,1,1}};
solution.setZeroes(arr);
System.out.println(Arrays.toString(arr));
}
static class Solution {
public void setZeroes(int[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
boolean [][] flags = new boolean[matrix.length][matrix[0].length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
if (matrix[i][j] == 0) {
dfs(matrix,flags,i, j);
}
}
}
for (int i = 0; i < flags.length; i++) {
for (int j = 0; j < flags[0].length; j++) {
if (flags[i][j]) {
matrix[i][j] = 0;
}
}
}
}
private void dfs(int[][] matrix, boolean[][] flags, int i, int j) {
if (matrix[i][j] == 0) flags[i][j] = true;
for (int k = 0; k < i; k++) {
flags[k][j] = true;
}
for (int k = i + 1; k < flags.length; k++) {
flags[k][j] = true;
}
for (int k = 0; k < j; k++) {
flags[i][k] = true;
}
for (int k = j + 1; k < flags[0].length; k++) {
flags[i][k] = true;
}
}
}
}