Skip to content

Commit 69481da

Browse files
committed
fd
1 parent 3d5a428 commit 69481da

File tree

3 files changed

+42
-21
lines changed

3 files changed

+42
-21
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@
373373
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/)|[Java](leetcode/solution/src/ConvertBSTToGreaterTree.java)|100||
374374
|541|[Reverse String II](https://leetcode.com/problems/reverse-string-ii/)| [Java](leetcode/solution/src/ReverseStringII.java)|100||
375375
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/)|[Java](leetcode/solution/src/DiameterOfBinaryTree.java)|80|这题易错|
376+
|547|[Friend Circles](https://leetcode.com/problems/friend-circles/)|[Java](leetcode/solution/src/FriendCircles.java)|70||
376377
|557|[Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/)| [Java](leetcode/solution/src/ReverseWordsInAStringIII.java)|100||
377378
|560|[Subarray Sum Equals K](https://leetcode.com/problems/subarray-sum-equals-k/)| [Java](leetcode/solution/src/SubarraySumEqualsK.java)|70||
378379
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Java](leetcode/solution/src/ConstructStringFromBinaryTree.java)|90||
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class FriendCircles {
2+
3+
public int findCircleNum(int[][] M) {
4+
int num = 0;
5+
for (int i = 0; i < M.length; i++) {
6+
if (M[i][i] == 1) {
7+
dfs(M, i);
8+
num++;
9+
}
10+
}
11+
return num;
12+
}
13+
14+
/**
15+
* M[i][i] = 0表示第i个人我们已经访问过了
16+
* 访问过的人无需重复访问
17+
*/
18+
private void dfs(int[][] M, int i) {
19+
M[i][i] = 0;
20+
for (int j = 0; j < M.length; j++) {
21+
if (M[j][j] != 0 && M[i][j] == 1) {
22+
dfs(M, j);
23+
}
24+
}
25+
}
26+
}

leetcode/src/Main.java

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,26 @@
22

33
public class Main {
44

5-
public static int totalFruit(int[] tree) {
6-
int max = 0;
7-
HashMap<Integer, Integer> map = new HashMap<>();
8-
for (int i = 0, j = 0; j < tree.length; j++) {
9-
int value = tree[j];
10-
11-
map.put(value, map.getOrDefault(value, 0) + 1);
12-
13-
for ( ; map.size() > 2; i++) {
14-
int val = tree[i], cnt = map.get(val);
15-
if (cnt == 1) {
16-
map.remove(val);
17-
} else {
18-
map.put(val, cnt - 1);
19-
}
5+
public int findCircleNum(int[][] M) {
6+
int num = 0;
7+
for (int i = 0; i < M.length; i++) {
8+
if (M[i][i] == 1) {
9+
dfs(M, i);
10+
num++;
2011
}
21-
max = Math.max(max, j - i + 1);
2212
}
13+
return num;
14+
}
2315

24-
return max;
16+
private void dfs(int[][] M, int i) {
17+
M[i][i] = 0;
18+
for (int j = 0; j < M.length; j++) {
19+
if (M[j][j] != 0 && M[i][j] == 1) {
20+
dfs(M, j);
21+
}
22+
}
2523
}
2624

2725
public static void main(String[] args) {
28-
int n = totalFruit(new int[]{
29-
1, 2, 1
30-
});
31-
System.out.println(n);
3226
}
3327
}

0 commit comments

Comments
 (0)