Skip to content

Commit e15b28b

Browse files
authored
feat: add rust solution to lc problem: No.1971 (#1131)
1 parent 14dd5d8 commit e15b28b

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

solution/1900-1999/1971.Find if Path Exists in Graph/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,39 @@ public:
326326
};
327327
```
328328

329+
### **Rust**
330+
331+
```rust
332+
impl Solution {
333+
pub fn valid_path(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32) -> bool {
334+
let mut disjoint_set: Vec<i32> = vec![0; n as usize];
335+
// Initialize the set
336+
for i in 0..n {
337+
disjoint_set[i as usize] = i;
338+
}
339+
340+
// Traverse the edges
341+
for p_vec in &edges {
342+
let parent_one = Solution::find(p_vec[0], &mut disjoint_set);
343+
let parent_two = Solution::find(p_vec[1], &mut disjoint_set);
344+
disjoint_set[parent_one as usize] = parent_two;
345+
}
346+
347+
let p_s = Solution::find(source, &mut disjoint_set);
348+
let p_d = Solution::find(destination, &mut disjoint_set);
349+
350+
p_s == p_d
351+
}
352+
353+
pub fn find(x: i32, d_set: &mut Vec<i32>) -> i32 {
354+
if d_set[x as usize] != x {
355+
d_set[x as usize] = Solution::find(d_set[x as usize], d_set);
356+
}
357+
d_set[x as usize]
358+
}
359+
}
360+
```
361+
329362
### **Go**
330363

331364
```go

solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md

+33
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,39 @@ public:
189189
};
190190
```
191191

192+
### **Rust**
193+
194+
```rust
195+
impl Solution {
196+
pub fn valid_path(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32) -> bool {
197+
let mut disjoint_set: Vec<i32> = vec![0; n as usize];
198+
// Initialize the set
199+
for i in 0..n {
200+
disjoint_set[i as usize] = i;
201+
}
202+
203+
// Traverse the edges
204+
for p_vec in &edges {
205+
let parent_one = Solution::find(p_vec[0], &mut disjoint_set);
206+
let parent_two = Solution::find(p_vec[1], &mut disjoint_set);
207+
disjoint_set[parent_one as usize] = parent_two;
208+
}
209+
210+
let p_s = Solution::find(source, &mut disjoint_set);
211+
let p_d = Solution::find(destination, &mut disjoint_set);
212+
213+
p_s == p_d
214+
}
215+
216+
pub fn find(x: i32, d_set: &mut Vec<i32>) -> i32 {
217+
if d_set[x as usize] != x {
218+
d_set[x as usize] = Solution::find(d_set[x as usize], d_set);
219+
}
220+
d_set[x as usize]
221+
}
222+
}
223+
```
224+
192225
### **Go**
193226

194227
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
impl Solution {
2+
pub fn valid_path(n: i32, edges: Vec<Vec<i32>>, source: i32, destination: i32) -> bool {
3+
let mut disjoint_set: Vec<i32> = vec![0; n as usize];
4+
// Initialize the set
5+
for i in 0..n {
6+
disjoint_set[i as usize] = i;
7+
}
8+
9+
// Traverse the edges
10+
for p_vec in &edges {
11+
let parent_one = Solution::find(p_vec[0], &mut disjoint_set);
12+
let parent_two = Solution::find(p_vec[1], &mut disjoint_set);
13+
disjoint_set[parent_one as usize] = parent_two;
14+
}
15+
16+
let p_s = Solution::find(source, &mut disjoint_set);
17+
let p_d = Solution::find(destination, &mut disjoint_set);
18+
19+
p_s == p_d
20+
}
21+
22+
pub fn find(x: i32, d_set: &mut Vec<i32>) -> i32 {
23+
if d_set[x as usize] != x {
24+
d_set[x as usize] = Solution::find(d_set[x as usize], d_set);
25+
}
26+
d_set[x as usize]
27+
}
28+
}

0 commit comments

Comments
 (0)