Skip to content

Commit f687179

Browse files
committed
feat: add new leetcode problems
1 parent ce3f14f commit f687179

File tree

302 files changed

+23049
-3473
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

302 files changed

+23049
-3473
lines changed

solution/0500-0599/0547.Friend Circles/README.md

-110
This file was deleted.

solution/0500-0599/0547.Friend Circles/README_EN.md

-132
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# [547. 省份数量](https://leetcode-cn.com/problems/number-of-provinces)
2+
3+
[English Version](/solution/0500-0599/0547.Number%20of%20Provinces/README_EN.md)
4+
5+
## 题目描述
6+
7+
<!-- 这里写题目描述 -->
8+
9+
<div class="original__bRMd">
10+
<div>
11+
<p>有 <code>n</code> 个城市,其中一些彼此相连,另一些没有相连。如果城市 <code>a</code> 与城市 <code>b</code> 直接相连,且城市 <code>b</code> 与城市 <code>c</code> 直接相连,那么城市 <code>a</code> 与城市 <code>c</code> 间接相连。</p>
12+
13+
<p><strong>省份</strong> 是一组直接或间接相连的城市,组内不含其他没有相连的城市。</p>
14+
15+
<p>给你一个 <code>n x n</code> 的矩阵 <code>isConnected</code> ,其中 <code>isConnected[i][j] = 1</code> 表示第 <code>i</code> 个城市和第 <code>j</code> 个城市直接相连,而 <code>isConnected[i][j] = 0</code> 表示二者不直接相连。</p>
16+
17+
<p>返回矩阵中 <strong>省份</strong> 的数量。</p>
18+
19+
<p> </p>
20+
21+
<p><strong>示例 1:</strong></p>
22+
23+
![](./images/graph1.jpg)
24+
25+
<pre>
26+
<strong>输入:</strong>isConnected = [[1,1,0],[1,1,0],[0,0,1]]
27+
<strong>输出:</strong>2
28+
</pre>
29+
30+
<p><strong>示例 2:</strong></p>
31+
32+
![](./images/graph2.jpg)
33+
34+
<pre>
35+
<strong>输入:</strong>isConnected = [[1,0,0],[0,1,0],[0,0,1]]
36+
<strong>输出:</strong>3
37+
</pre>
38+
39+
<p> </p>
40+
41+
<p><strong>提示:</strong></p>
42+
43+
<ul>
44+
<li><code>1 <= n <= 200</code></li>
45+
<li><code>n == isConnected.length</code></li>
46+
<li><code>n == isConnected[i].length</code></li>
47+
<li><code>isConnected[i][j]</code> 为 <code>1</code> 或 <code>0</code></li>
48+
<li><code>isConnected[i][i] == 1</code></li>
49+
<li><code>isConnected[i][j] == isConnected[j][i]</code></li>
50+
</ul>
51+
</div>
52+
</div>
53+
54+
55+
## 解法
56+
57+
<!-- 这里可写通用的实现逻辑 -->
58+
59+
深度优先搜索。判断学生与学生之间是否属于同一个连通分量,最后连通分量的总数即为结果。
60+
61+
<!-- tabs:start -->
62+
63+
### **Python3**
64+
65+
<!-- 这里可写当前语言的特殊实现逻辑 -->
66+
67+
```python
68+
class Solution:
69+
def findCircleNum(self, isConnected: List[List[int]]) -> int:
70+
def dfs(i):
71+
for j in range(n):
72+
if not visited[j] and isConnected[i][j] == 1:
73+
visited[j] = True
74+
dfs(j)
75+
76+
n = len(isConnected)
77+
visited = [False] * n
78+
num = 0
79+
for i in range(n):
80+
if not visited[i]:
81+
dfs(i)
82+
num += 1
83+
return num
84+
```
85+
86+
### **Java**
87+
88+
<!-- 这里可写当前语言的特殊实现逻辑 -->
89+
90+
```java
91+
class Solution {
92+
public int findCircleNum(int[][] isConnected) {
93+
int n = isConnected.length;
94+
boolean[] visited = new boolean[n];
95+
int num = 0;
96+
for (int i = 0; i < n; ++i) {
97+
if (!visited[i]) {
98+
dfs(isConnected, visited, i, n);
99+
++num;
100+
}
101+
}
102+
return num;
103+
}
104+
105+
private void dfs(int[][] isConnected, boolean[] visited, int i, int n) {
106+
for (int j = 0; j < n; ++j) {
107+
if (!visited[j] && isConnected[i][j] == 1) {
108+
visited[j] = true;
109+
dfs(isConnected, visited, j, n);
110+
}
111+
}
112+
}
113+
}
114+
```
115+
116+
### **...**
117+
118+
```
119+
120+
```
121+
122+
<!-- tabs:end -->

0 commit comments

Comments
 (0)