-
Notifications
You must be signed in to change notification settings - Fork 7
/
friend-circles.java
53 lines (50 loc) · 1.49 KB
/
friend-circles.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
41
42
43
44
45
46
47
48
49
50
51
52
53
/****************************************Union find solution***************************************/
public class Solution {
int[] parent = new int[];
int count;
public int findCircleNum(int[][] M) {
for (int i = 0; i < parent.length; i++) {
parent[i] = i;
}
count = M.length;
for (int i = 0; i < M.length; i++)
for (int j = 0; j < M[0].length; j++) {
if (M[i][j] == 1) union(i, j);
}
return count;
}
public void union(int i, int j) {
int root_i = find(i);
int root_j= find(j;
if (root_i == root_j) return;
parent[root_i] = parent[root_j];
count--; // still don't get this one
}
public int find(int i) {
while (i != parents[i]) {
parent[i = parent[parent[i]];
i = parent[i];
}
return i;
}
}
/********************DFS solution***************************/
public class Solution {
public int findCircleNum(int[][] M) {
boolean[] visited = new boolean[M.length];
for (int i = 0; i < M.length; i++) {
if (!visited[i]) {
count++;
dfs(M, visited, i);
}
}
return count;
}
public void dfs(int[][] M, boolean[] visited, int i) {
for (int j = 0; j < M.length; j++) {
if (M[i][j] && !visited[j]) {
dfs(M, visited, j);
}
}
}
}