-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendCircle.java
40 lines (34 loc) · 918 Bytes
/
FriendCircle.java
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
public class FriendCircle {
static int visit[];
static int counter = 0;
public static void main(String args[]) {
}
public static int findCircleNum(int[][] M) {
int result = 0;
if (M.length == 0) return 0;
visit = new int[M.length];
for (int i = 0; i < M.length; i++) {
visit[i] = 0;
}
for (int i = 0; i < M.length; i++) {
counter = 0;
if (visit[i] == 0) {
visit[i] = 1;
dfs(i, M);
if (counter > 0) {
result++;
}
}
}
return result;
}
static void dfs(int x, int[][] M) {
for (int i = 0; i < M[x].length; i++) {
if (visit[M[x][i]] == 0) {
visit[M[x][i]] = 1;
counter++;
dfs(M[x][i], M);
}
}
}
}