Skip to content

Commit 14c5a12

Browse files
committed
feat: update solutions to lc problem: No.0323
1 parent 761336b commit 14c5a12

File tree

6 files changed

+14
-65
lines changed

6 files changed

+14
-65
lines changed

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md

+5-22
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,7 @@ class Solution:
116116

117117
for a, b in edges:
118118
p[find(b)] = find(a)
119-
cnt = 0
120-
visit = [False] * n
121-
for i in range(n):
122-
if not visit[find(i)]:
123-
cnt += 1
124-
visit[find(i)] = True
125-
return cnt
119+
return sum(i == find(i) for i in range(n))
126120
```
127121

128122
### **Java**
@@ -132,7 +126,7 @@ class Solution:
132126
```java
133127
class Solution {
134128
private int[] p;
135-
129+
136130
public int countComponents(int n, int[][] edges) {
137131
p = new int[n];
138132
for (int i = 0; i < n; ++i) {
@@ -144,11 +138,9 @@ class Solution {
144138
}
145139

146140
int cnt = 0;
147-
boolean[] visit = new boolean[n];
148141
for (int i = 0; i < n; ++i) {
149-
if (!visit[find(i)]) {
142+
if (i == find(i)) {
150143
++cnt;
151-
visit[find(i)] = true;
152144
}
153145
}
154146
return cnt;
@@ -182,14 +174,10 @@ public:
182174
p[find(b)] = find(a);
183175
}
184176
int cnt = 0;
185-
vector<bool> visit(n, false);
186177
for (int i = 0; i < n; ++i)
187178
{
188-
if (!visit[find(i)])
189-
{
179+
if (i == find(i))
190180
++cnt;
191-
visit[find(i)] = true;
192-
}
193181
}
194182
return cnt;
195183
}
@@ -219,14 +207,9 @@ func countComponents(n int, edges [][]int) int {
219207
p[find(b)] = find(a)
220208
}
221209
cnt := 0
222-
visit := make([]bool, n)
223-
for i := 0; i < n; i++ {
224-
visit[i] = false
225-
}
226210
for i := 0; i < n; i++ {
227-
if !visit[find(i)] {
211+
if i == find(i) {
228212
cnt++
229-
visit[find(i)] = true
230213
}
231214
}
232215
return cnt

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README_EN.md

+5-22
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,15 @@ class Solution:
5353

5454
for a, b in edges:
5555
p[find(b)] = find(a)
56-
cnt = 0
57-
visit = [False] * n
58-
for i in range(n):
59-
if not visit[find(i)]:
60-
cnt += 1
61-
visit[find(i)] = True
62-
return cnt
56+
return sum(i == find(i) for i in range(n))
6357
```
6458

6559
### **Java**
6660

6761
```java
6862
class Solution {
6963
private int[] p;
70-
64+
7165
public int countComponents(int n, int[][] edges) {
7266
p = new int[n];
7367
for (int i = 0; i < n; ++i) {
@@ -79,11 +73,9 @@ class Solution {
7973
}
8074

8175
int cnt = 0;
82-
boolean[] visit = new boolean[n];
8376
for (int i = 0; i < n; ++i) {
84-
if (!visit[find(i)]) {
77+
if (i == find(i)) {
8578
++cnt;
86-
visit[find(i)] = true;
8779
}
8880
}
8981
return cnt;
@@ -117,14 +109,10 @@ public:
117109
p[find(b)] = find(a);
118110
}
119111
int cnt = 0;
120-
vector<bool> visit(n, false);
121112
for (int i = 0; i < n; ++i)
122113
{
123-
if (!visit[find(i)])
124-
{
114+
if (i == find(i))
125115
++cnt;
126-
visit[find(i)] = true;
127-
}
128116
}
129117
return cnt;
130118
}
@@ -154,14 +142,9 @@ func countComponents(n int, edges [][]int) int {
154142
p[find(b)] = find(a)
155143
}
156144
cnt := 0
157-
visit := make([]bool, n)
158-
for i := 0; i < n; i++ {
159-
visit[i] = false
160-
}
161145
for i := 0; i < n; i++ {
162-
if !visit[find(i)] {
146+
if i == find(i) {
163147
cnt++
164-
visit[find(i)] = true
165148
}
166149
}
167150
return cnt

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.cpp

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,10 @@ class Solution {
1414
p[find(b)] = find(a);
1515
}
1616
int cnt = 0;
17-
vector<bool> visit(n, false);
1817
for (int i = 0; i < n; ++i)
1918
{
20-
if (!visit[find(i)])
21-
{
19+
if (i == find(i))
2220
++cnt;
23-
visit[find(i)] = true;
24-
}
2521
}
2622
return cnt;
2723
}

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.go

+1-6
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,9 @@ func countComponents(n int, edges [][]int) int {
1010
p[find(b)] = find(a)
1111
}
1212
cnt := 0
13-
visit := make([]bool, n)
1413
for i := 0; i < n; i++ {
15-
visit[i] = false
16-
}
17-
for i := 0; i < n; i++ {
18-
if !visit[find(i)] {
14+
if i == find(i) {
1915
cnt++
20-
visit[find(i)] = true
2116
}
2217
}
2318
return cnt

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@ public int countComponents(int n, int[][] edges) {
1212
}
1313

1414
int cnt = 0;
15-
boolean[] visit = new boolean[n];
1615
for (int i = 0; i < n; ++i) {
17-
if (!visit[find(i)]) {
16+
if (i == find(i)) {
1817
++cnt;
19-
visit[find(i)] = true;
2018
}
2119
}
2220
return cnt;

solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.py

+1-7
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,4 @@ def find(x):
99

1010
for a, b in edges:
1111
p[find(b)] = find(a)
12-
cnt = 0
13-
visit = [False] * n
14-
for i in range(n):
15-
if not visit[find(i)]:
16-
cnt += 1
17-
visit[find(i)] = True
18-
return cnt
12+
return sum(i == find(i) for i in range(n))

0 commit comments

Comments
 (0)