Skip to content

Commit f8cdf2c

Browse files
committed
feat: add solution to lc problem: No.0684
No.0684.Redundant Connection
1 parent f6aeec6 commit f8cdf2c

File tree

4 files changed

+73
-3
lines changed

4 files changed

+73
-3
lines changed

Diff for: solution/0600-0699/0684.Redundant Connection/README.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -207,14 +207,39 @@ func findRedundantConnection(edges [][]int) []int {
207207
for _, e := range edges {
208208
a, b := e[0], e[1]
209209
if find(a) == find(b) {
210-
return []int{a, b}
210+
return e
211211
}
212212
p[find(a)] = find(b)
213213
}
214214
return []int{}
215215
}
216216
```
217217

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+
218243
### **...**
219244

220245
```

Diff for: solution/0600-0699/0684.Redundant Connection/README_EN.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,39 @@ func findRedundantConnection(edges [][]int) []int {
174174
for _, e := range edges {
175175
a, b := e[0], e[1]
176176
if find(a) == find(b) {
177-
return []int{a, b}
177+
return e
178178
}
179179
p[find(a)] = find(b)
180180
}
181181
return []int{}
182182
}
183183
```
184184

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+
185210
### **...**
186211

187212
```

Diff for: solution/0600-0699/0684.Redundant Connection/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func findRedundantConnection(edges [][]int) []int {
1313
for _, e := range edges {
1414
a, b := e[0], e[1]
1515
if find(a) == find(b) {
16-
return []int{a, b}
16+
return e
1717
}
1818
p[find(a)] = find(b)
1919
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
};

0 commit comments

Comments
 (0)