From ccc76da59c3949828af6469b67123734caf3cd3c Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Mon, 3 Jul 2023 22:14:06 +0800 Subject: [PATCH] feat: Add rust implementation for NO.1971 --- .../README.md | 33 +++++++++++++++++++ .../README_EN.md | 33 +++++++++++++++++++ .../Solution.rs | 28 ++++++++++++++++ 3 files changed, 94 insertions(+) create mode 100644 solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs diff --git a/solution/1900-1999/1971.Find if Path Exists in Graph/README.md b/solution/1900-1999/1971.Find if Path Exists in Graph/README.md index 9079f085f6067..3b69685d40323 100644 --- a/solution/1900-1999/1971.Find if Path Exists in Graph/README.md +++ b/solution/1900-1999/1971.Find if Path Exists in Graph/README.md @@ -326,6 +326,39 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + pub fn valid_path(n: i32, edges: Vec>, source: i32, destination: i32) -> bool { + let mut disjoint_set: Vec = vec![0; n as usize]; + // Initialize the set + for i in 0..n { + disjoint_set[i as usize] = i; + } + + // Traverse the edges + for p_vec in &edges { + let parent_one = Solution::find(p_vec[0], &mut disjoint_set); + let parent_two = Solution::find(p_vec[1], &mut disjoint_set); + disjoint_set[parent_one as usize] = parent_two; + } + + let p_s = Solution::find(source, &mut disjoint_set); + let p_d = Solution::find(destination, &mut disjoint_set); + + p_s == p_d + } + + pub fn find(x: i32, d_set: &mut Vec) -> i32 { + if d_set[x as usize] != x { + d_set[x as usize] = Solution::find(d_set[x as usize], d_set); + } + d_set[x as usize] + } +} +``` + ### **Go** ```go diff --git a/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md b/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md index 5f3b0bab3ac5b..deae972d73c75 100644 --- a/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md +++ b/solution/1900-1999/1971.Find if Path Exists in Graph/README_EN.md @@ -189,6 +189,39 @@ public: }; ``` +### **Rust** + +```rust +impl Solution { + pub fn valid_path(n: i32, edges: Vec>, source: i32, destination: i32) -> bool { + let mut disjoint_set: Vec = vec![0; n as usize]; + // Initialize the set + for i in 0..n { + disjoint_set[i as usize] = i; + } + + // Traverse the edges + for p_vec in &edges { + let parent_one = Solution::find(p_vec[0], &mut disjoint_set); + let parent_two = Solution::find(p_vec[1], &mut disjoint_set); + disjoint_set[parent_one as usize] = parent_two; + } + + let p_s = Solution::find(source, &mut disjoint_set); + let p_d = Solution::find(destination, &mut disjoint_set); + + p_s == p_d + } + + pub fn find(x: i32, d_set: &mut Vec) -> i32 { + if d_set[x as usize] != x { + d_set[x as usize] = Solution::find(d_set[x as usize], d_set); + } + d_set[x as usize] + } +} +``` + ### **Go** ```go diff --git a/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs b/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs new file mode 100644 index 0000000000000..f42912594c198 --- /dev/null +++ b/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.rs @@ -0,0 +1,28 @@ +impl Solution { + pub fn valid_path(n: i32, edges: Vec>, source: i32, destination: i32) -> bool { + let mut disjoint_set: Vec = vec![0; n as usize]; + // Initialize the set + for i in 0..n { + disjoint_set[i as usize] = i; + } + + // Traverse the edges + for p_vec in &edges { + let parent_one = Solution::find(p_vec[0], &mut disjoint_set); + let parent_two = Solution::find(p_vec[1], &mut disjoint_set); + disjoint_set[parent_one as usize] = parent_two; + } + + let p_s = Solution::find(source, &mut disjoint_set); + let p_d = Solution::find(destination, &mut disjoint_set); + + p_s == p_d + } + + pub fn find(x: i32, d_set: &mut Vec) -> i32 { + if d_set[x as usize] != x { + d_set[x as usize] = Solution::find(d_set[x as usize], d_set); + } + d_set[x as usize] + } +} \ No newline at end of file