File tree 4 files changed +73
-3
lines changed
solution/0600-0699/0684.Redundant Connection
4 files changed +73
-3
lines changed Original file line number Diff line number Diff line change @@ -207,14 +207,39 @@ func findRedundantConnection(edges [][]int) []int {
207
207
for _, e := range edges {
208
208
a, b := e[0], e[1]
209
209
if find(a) == find(b) {
210
- return []int{a, b}
210
+ return e
211
211
}
212
212
p[find(a)] = find(b)
213
213
}
214
214
return []int{}
215
215
}
216
216
```
217
217
218
+ ### ** JavaScript**
219
+
220
+ ``` js
221
+ /**
222
+ * @param {number[][]} edges
223
+ * @return {number[]}
224
+ */
225
+ var findRedundantConnection = function (edges ) {
226
+ let p = Array .from ({ length: 1010 }, (_ , i ) => i);
227
+ function find (x ) {
228
+ if (p[x] != x) {
229
+ p[x] = find (p[x]);
230
+ }
231
+ return p[x];
232
+ }
233
+ for (let [a, b] of edges) {
234
+ if (find (a) == find (b)) {
235
+ return [a, b];
236
+ }
237
+ p[find (a)] = find (b);
238
+ }
239
+ return [];
240
+ };
241
+ ```
242
+
218
243
### ** ...**
219
244
220
245
```
Original file line number Diff line number Diff line change @@ -174,14 +174,39 @@ func findRedundantConnection(edges [][]int) []int {
174
174
for _, e := range edges {
175
175
a, b := e[0], e[1]
176
176
if find(a) == find(b) {
177
- return []int{a, b}
177
+ return e
178
178
}
179
179
p[find(a)] = find(b)
180
180
}
181
181
return []int{}
182
182
}
183
183
```
184
184
185
+ ### ** JavaScript**
186
+
187
+ ``` js
188
+ /**
189
+ * @param {number[][]} edges
190
+ * @return {number[]}
191
+ */
192
+ var findRedundantConnection = function (edges ) {
193
+ let p = Array .from ({ length: 1010 }, (_ , i ) => i);
194
+ function find (x ) {
195
+ if (p[x] != x) {
196
+ p[x] = find (p[x]);
197
+ }
198
+ return p[x];
199
+ }
200
+ for (let [a, b] of edges) {
201
+ if (find (a) == find (b)) {
202
+ return [a, b];
203
+ }
204
+ p[find (a)] = find (b);
205
+ }
206
+ return [];
207
+ };
208
+ ```
209
+
185
210
### ** ...**
186
211
187
212
```
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ func findRedundantConnection(edges [][]int) []int {
13
13
for _ , e := range edges {
14
14
a , b := e [0 ], e [1 ]
15
15
if find (a ) == find (b ) {
16
- return [] int { a , b }
16
+ return e
17
17
}
18
18
p [find (a )] = find (b )
19
19
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number[][] } edges
3
+ * @return {number[] }
4
+ */
5
+ var findRedundantConnection = function ( edges ) {
6
+ let p = Array . from ( { length : 1010 } , ( _ , i ) => i ) ;
7
+ function find ( x ) {
8
+ if ( p [ x ] != x ) {
9
+ p [ x ] = find ( p [ x ] ) ;
10
+ }
11
+ return p [ x ] ;
12
+ }
13
+ for ( let [ a , b ] of edges ) {
14
+ if ( find ( a ) == find ( b ) ) {
15
+ return [ a , b ] ;
16
+ }
17
+ p [ find ( a ) ] = find ( b ) ;
18
+ }
19
+ return [ ] ;
20
+ } ;
You can’t perform that action at this time.
0 commit comments