File tree 6 files changed +14
-65
lines changed
solution/0300-0399/0323.Number of Connected Components in an Undirected Graph
6 files changed +14
-65
lines changed Original file line number Diff line number Diff line change @@ -116,13 +116,7 @@ class Solution:
116
116
117
117
for a, b in edges:
118
118
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))
126
120
```
127
121
128
122
### ** Java**
@@ -132,7 +126,7 @@ class Solution:
132
126
``` java
133
127
class Solution {
134
128
private int [] p;
135
-
129
+
136
130
public int countComponents (int n , int [][] edges ) {
137
131
p = new int [n];
138
132
for (int i = 0 ; i < n; ++ i) {
@@ -144,11 +138,9 @@ class Solution {
144
138
}
145
139
146
140
int cnt = 0 ;
147
- boolean [] visit = new boolean [n];
148
141
for (int i = 0 ; i < n; ++ i) {
149
- if (! visit[ find(i)] ) {
142
+ if (i == find(i)) {
150
143
++ cnt;
151
- visit[find(i)] = true ;
152
144
}
153
145
}
154
146
return cnt;
@@ -182,14 +174,10 @@ public:
182
174
p[find(b)] = find(a);
183
175
}
184
176
int cnt = 0;
185
- vector<bool> visit(n, false);
186
177
for (int i = 0; i < n; ++i)
187
178
{
188
- if (!visit[find(i)])
189
- {
179
+ if (i == find(i))
190
180
++cnt;
191
- visit[find(i)] = true;
192
- }
193
181
}
194
182
return cnt;
195
183
}
@@ -219,14 +207,9 @@ func countComponents(n int, edges [][]int) int {
219
207
p[find (b)] = find (a)
220
208
}
221
209
cnt := 0
222
- visit := make ([]bool , n)
223
- for i := 0 ; i < n; i++ {
224
- visit[i] = false
225
- }
226
210
for i := 0 ; i < n; i++ {
227
- if !visit[ find (i)] {
211
+ if i == find (i) {
228
212
cnt++
229
- visit[find (i)] = true
230
213
}
231
214
}
232
215
return cnt
Original file line number Diff line number Diff line change @@ -53,21 +53,15 @@ class Solution:
53
53
54
54
for a, b in edges:
55
55
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))
63
57
```
64
58
65
59
### ** Java**
66
60
67
61
``` java
68
62
class Solution {
69
63
private int [] p;
70
-
64
+
71
65
public int countComponents (int n , int [][] edges ) {
72
66
p = new int [n];
73
67
for (int i = 0 ; i < n; ++ i) {
@@ -79,11 +73,9 @@ class Solution {
79
73
}
80
74
81
75
int cnt = 0 ;
82
- boolean [] visit = new boolean [n];
83
76
for (int i = 0 ; i < n; ++ i) {
84
- if (! visit[ find(i)] ) {
77
+ if (i == find(i)) {
85
78
++ cnt;
86
- visit[find(i)] = true ;
87
79
}
88
80
}
89
81
return cnt;
@@ -117,14 +109,10 @@ public:
117
109
p[find(b)] = find(a);
118
110
}
119
111
int cnt = 0;
120
- vector<bool> visit(n, false);
121
112
for (int i = 0; i < n; ++i)
122
113
{
123
- if (!visit[find(i)])
124
- {
114
+ if (i == find(i))
125
115
++cnt;
126
- visit[find(i)] = true;
127
- }
128
116
}
129
117
return cnt;
130
118
}
@@ -154,14 +142,9 @@ func countComponents(n int, edges [][]int) int {
154
142
p[find (b)] = find (a)
155
143
}
156
144
cnt := 0
157
- visit := make ([]bool , n)
158
- for i := 0 ; i < n; i++ {
159
- visit[i] = false
160
- }
161
145
for i := 0 ; i < n; i++ {
162
- if !visit[ find (i)] {
146
+ if i == find (i) {
163
147
cnt++
164
- visit[find (i)] = true
165
148
}
166
149
}
167
150
return cnt
Original file line number Diff line number Diff line change @@ -14,14 +14,10 @@ class Solution {
14
14
p[find (b)] = find (a);
15
15
}
16
16
int cnt = 0 ;
17
- vector<bool > visit (n, false );
18
17
for (int i = 0 ; i < n; ++i)
19
18
{
20
- if (!visit[find (i)])
21
- {
19
+ if (i == find (i))
22
20
++cnt;
23
- visit[find (i)] = true ;
24
- }
25
21
}
26
22
return cnt;
27
23
}
Original file line number Diff line number Diff line change @@ -10,14 +10,9 @@ func countComponents(n int, edges [][]int) int {
10
10
p [find (b )] = find (a )
11
11
}
12
12
cnt := 0
13
- visit := make ([]bool , n )
14
13
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 ) {
19
15
cnt ++
20
- visit [find (i )] = true
21
16
}
22
17
}
23
18
return cnt
Original file line number Diff line number Diff line change @@ -12,11 +12,9 @@ public int countComponents(int n, int[][] edges) {
12
12
}
13
13
14
14
int cnt = 0 ;
15
- boolean [] visit = new boolean [n ];
16
15
for (int i = 0 ; i < n ; ++i ) {
17
- if (! visit [ find (i )] ) {
16
+ if (i == find (i )) {
18
17
++cnt ;
19
- visit [find (i )] = true ;
20
18
}
21
19
}
22
20
return cnt ;
Original file line number Diff line number Diff line change @@ -9,10 +9,4 @@ def find(x):
9
9
10
10
for a , b in edges :
11
11
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 ))
You can’t perform that action at this time.
0 commit comments