-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_547_1.java
More file actions
39 lines (34 loc) · 951 Bytes
/
LeetCode_547_1.java
File metadata and controls
39 lines (34 loc) · 951 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
package id_1;
/**
* @创建人 luoxiang
* @创建时间 2019/6/20 12:44
* @描述 LeetCode : 547. 朋友圈 https://leetcode-cn.com/problems/friend-circles/
*/
public class LeetCode_547_1 {
public static void main(String[] args) {
}
/**
* Method 1 : DFS 深度优先
*/
public int findCircleNum(int[][] M) {
if (M == null || M.length == 0) return 0;
int count = 0;
for (int i = 0; i < M.length; i++) {
for (int j = 0; j < M.length; j++) {
if (M[i][j] == 1) {
count++;
dfs(M, i, j);
}
}
}
return count;
}
private void dfs(int[][] m, int i, int j) {
if (i < 0 || j < 0 || i >= m.length || j >= m.length || m[i][j] == 0) return;
m[i][j] = 0;
dfs(m, i + 1, j);
dfs(m, i - 1, j);
dfs(m, i, j + 1);
dfs(m, i, j - 1);
}
}