Skip to content

Commit 37f3203

Browse files
committed
feat(leetcode): add No.547
1 parent 66e84e8 commit 37f3203

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

547.Friend-Circles.java

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// https://leetcode.com/problems/friend-circles/
2+
//
3+
// algorithms
4+
// Medium (53.54%)
5+
// Total Accepted: 84,688
6+
// Total Submissions: 158,175
7+
// beats 100.0% of java submissions
8+
9+
class Solution {
10+
public int findCircleNum(int[][] M) {
11+
if (M == null) {
12+
return 0;
13+
}
14+
int n = M.length;
15+
if (n == 0) {
16+
return 0;
17+
}
18+
19+
int flag = 2;
20+
for (int i = 0; i < n; i++) {
21+
if (M[i][i] == 1) {
22+
recursive(M, i, flag);
23+
flag++;
24+
}
25+
}
26+
27+
return flag - 2;
28+
}
29+
30+
public void recursive(int[][] M, int idx, int flag) {
31+
int n = M.length;
32+
M[idx][idx] = flag;
33+
34+
for (int i = 0; i < n; i++) {
35+
if (M[idx][i] == 1 && M[i][i] == 1) {
36+
recursive(M, i, flag);
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)