-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfindCircleNum.java
More file actions
41 lines (38 loc) · 1.05 KB
/
findCircleNum.java
File metadata and controls
41 lines (38 loc) · 1.05 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
/**
* Author : WindAsMe
* File : findCircleNum.java
* Time : Create on 18-9-13
* Location : ../Home/JavaForLeeCode2/findCircleNum.java
* Function : LeetCode No.547
*/
public class findCircleNum {
private static int findCircleNumResult(int[][] M) {
if (M.length == 0 || M[0].length == 0)
return 0;
int count = 0;
boolean[] flag = new boolean[M.length];
for (int i = 0; i < M.length; i++) {
if (!flag[i]) {
dfs(M, i, flag);
count++;
}
}
return count;
}
private static void dfs(int[][] M, int i, boolean[] flag) {
for (int j = 0; j < M[0].length; j++) {
if (M[i][j] == 1 && !flag[j]) {
flag[j] = true;
dfs(M, j, flag);
}
}
}
public static void main(String[] args) {
int[][] nums = {
{1,0,0},
{0,1,0},
{0,0,1}
};
System.out.println(findCircleNumResult(nums));
}
}