leetcode-1582. 二进制矩阵中的特殊位置

原始思路

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
class Solution {
int count = 0;
public int numSpecial(int[][] mat) {
int row = mat.length;
int cols = mat[0].length;

for (int i = 0; i < row; i++) {
for (int j = 0; j < cols; j++) {
if (mat[i][j] == 1) {
dfs(mat, i, j, row, cols);
}
}
}
return count;
}

public void dfs(int[][] mat, int i, int j, int row, int cols) {
boolean rowBool = true;
for (int k = 0; k < row; k++) {
if (k == i) {
continue;
}
if (mat[k][j] == 1){
rowBool = false;
break;
}
}

boolean colBool = true;
for (int k = 0; k < cols; k++) {
if (k == j) {
continue;
}
if (mat[i][k] == 1){
colBool = false;
break;
}
}

if (rowBool && colBool) {
count++;
}
}
}