-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
44 lines (40 loc) · 970 Bytes
/
Copy pathSolution.java
File metadata and controls
44 lines (40 loc) · 970 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
36
37
38
39
40
41
42
43
44
import java.util.LinkedList;
import java.util.Queue;
public class Solution
{
public int findCircleNum(int[][] M)
{
int n = M.length;
boolean[] visited = new boolean[n];
int cnt = 0;
for (int i = 0; i < n; i++)
{
if (!visited[i])
{
visit(i, visited, M, n);
cnt++;
}
}
return cnt;
}
private void visit(int person, boolean[] visited, int[][] m, int n)
{
Queue<Integer> queue = new LinkedList<Integer>();
queue.add(person);
while (!queue.isEmpty())
{
int p = queue.poll();
if (!visited[p])
{
visited[p] = true;
for (int i = 0; i < n; i++)
{
if (m[p][i] == 1)
{
queue.add(i);
}
}
}
}
}
}