File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments