-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path547.java
More file actions
24 lines (24 loc) · 704 Bytes
/
547.java
File metadata and controls
24 lines (24 loc) · 704 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
class Solution {
public int findCircleNum(int[][] M) {
int n = M.length;
Set<Integer> student = new HashSet<Integer>();
int ans = 0;
for (int i = 0; i < n; i++) {
if (student.contains(i)) continue;
List<Integer> l = new ArrayList<Integer>();
l.add(i);
ans++;
int j = 0;
while (j < l.size()) {
for (int k = 0; k < n; k++) {
if (M[l.get(j)][k] == 1 && !student.contains(k)) {
student.add(k);
l.add(k);
}
}
j++;
}
}
return ans;
}
}