diff --git a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md index 00faf635c6685..eedc731b91d30 100644 --- a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md +++ b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md @@ -62,8 +62,8 @@ tags: - (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,3,3] 。 - (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,3] 。 - (2, 2) 是一组非互质数,且 LCM(2, 2) = 2 。得到 nums = [2,1,1,3] 。 -现在,nums 中不存在相邻的非互质数。 -因此,修改后得到的最终数组是 [2,1,1,3] 。 +现在,nums 中不存在相邻的非互质数。 +因此,修改后得到的最终数组是 [2,1,1,3] 。 注意,存在其他方法可以获得相同的最终数组。 @@ -229,6 +229,79 @@ function replaceNonCoprimes(nums: number[]): number[] { } ``` +#### Rust + +```rust +impl Solution { + pub fn replace_non_coprimes(nums: Vec) -> Vec { + fn gcd(mut a: i64, mut b: i64) -> i64 { + while b != 0 { + let t = a % b; + a = b; + b = t; + } + a + } + + let mut stk: Vec = Vec::new(); + for x in nums { + stk.push(x as i64); + while stk.len() > 1 { + let x = *stk.last().unwrap(); + let y = stk[stk.len() - 2]; + let g = gcd(x, y); + if g == 1 { + break; + } + stk.pop(); + let last = stk.last_mut().unwrap(); + *last = x / g * y; + } + } + + stk.into_iter().map(|v| v as i32).collect() + } +} +``` + +#### C# + +```cs +public class Solution { + public IList ReplaceNonCoprimes(int[] nums) { + long Gcd(long a, long b) { + while (b != 0) { + long t = a % b; + a = b; + b = t; + } + return a; + } + + var stk = new List(); + foreach (int num in nums) { + stk.Add(num); + while (stk.Count > 1) { + long x = stk[stk.Count - 1]; + long y = stk[stk.Count - 2]; + long g = Gcd(x, y); + if (g == 1) { + break; + } + stk.RemoveAt(stk.Count - 1); + stk[stk.Count - 1] = x / g * y; + } + } + + var ans = new List(); + foreach (long v in stk) { + ans.Add((int)v); + } + return ans; + } +} +``` + diff --git a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md index 35de06154de56..00b850cde7ece 100644 --- a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md +++ b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md @@ -42,7 +42,7 @@ tags:
 Input: nums = [6,4,3,2,7,6,2]
 Output: [12,7,6]
-Explanation: 
+Explanation:
 - (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [12,3,2,7,6,2].
 - (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [12,2,7,6,2].
 - (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [12,7,6,2].
@@ -57,7 +57,7 @@ Note that there are other ways to obtain the same resultant array.
 
 Input: nums = [2,2,1,1,3,3,3]
 Output: [2,1,1,3]
-Explanation: 
+Explanation:
 - (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3,3].
 - (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,3].
 - (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [2,1,1,3].
@@ -227,6 +227,79 @@ function replaceNonCoprimes(nums: number[]): number[] {
 }
 ```
 
+#### Rust
+
+```rust
+impl Solution {
+    pub fn replace_non_coprimes(nums: Vec) -> Vec {
+        fn gcd(mut a: i64, mut b: i64) -> i64 {
+            while b != 0 {
+                let t = a % b;
+                a = b;
+                b = t;
+            }
+            a
+        }
+
+        let mut stk: Vec = Vec::new();
+        for x in nums {
+            stk.push(x as i64);
+            while stk.len() > 1 {
+                let x = *stk.last().unwrap();
+                let y = stk[stk.len() - 2];
+                let g = gcd(x, y);
+                if g == 1 {
+                    break;
+                }
+                stk.pop();
+                let last = stk.last_mut().unwrap();
+                *last = x / g * y;
+            }
+        }
+
+        stk.into_iter().map(|v| v as i32).collect()
+    }
+}
+```
+
+#### C#
+
+```cs
+public class Solution {
+    public IList ReplaceNonCoprimes(int[] nums) {
+        long Gcd(long a, long b) {
+            while (b != 0) {
+                long t = a % b;
+                a = b;
+                b = t;
+            }
+            return a;
+        }
+
+        var stk = new List();
+        foreach (int num in nums) {
+            stk.Add(num);
+            while (stk.Count > 1) {
+                long x = stk[stk.Count - 1];
+                long y = stk[stk.Count - 2];
+                long g = Gcd(x, y);
+                if (g == 1) {
+                    break;
+                }
+                stk.RemoveAt(stk.Count - 1);
+                stk[stk.Count - 1] = x / g * y;
+            }
+        }
+
+        var ans = new List();
+        foreach (long v in stk) {
+            ans.Add((int)v);
+        }
+        return ans;
+    }
+}
+```
+
 
 
 
diff --git a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/Solution.cs b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/Solution.cs
new file mode 100644
index 0000000000000..1e24874ad1ac9
--- /dev/null
+++ b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/Solution.cs	
@@ -0,0 +1,33 @@
+public class Solution {
+    public IList ReplaceNonCoprimes(int[] nums) {
+        long Gcd(long a, long b) {
+            while (b != 0) {
+                long t = a % b;
+                a = b;
+                b = t;
+            }
+            return a;
+        }
+
+        var stk = new List();
+        foreach (int num in nums) {
+            stk.Add(num);
+            while (stk.Count > 1) {
+                long x = stk[stk.Count - 1];
+                long y = stk[stk.Count - 2];
+                long g = Gcd(x, y);
+                if (g == 1) {
+                    break;
+                }
+                stk.RemoveAt(stk.Count - 1);
+                stk[stk.Count - 1] = x / g * y;
+            }
+        }
+
+        var ans = new List();
+        foreach (long v in stk) {
+            ans.Add((int)v);
+        }
+        return ans;
+    }
+}
diff --git a/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/Solution.rs b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/Solution.rs
new file mode 100644
index 0000000000000..a050e3fb6c8c2
--- /dev/null
+++ b/solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/Solution.rs	
@@ -0,0 +1,30 @@
+impl Solution {
+    pub fn replace_non_coprimes(nums: Vec) -> Vec {
+        fn gcd(mut a: i64, mut b: i64) -> i64 {
+            while b != 0 {
+                let t = a % b;
+                a = b;
+                b = t;
+            }
+            a
+        }
+
+        let mut stk: Vec = Vec::new();
+        for x in nums {
+            stk.push(x as i64);
+            while stk.len() > 1 {
+                let x = *stk.last().unwrap();
+                let y = stk[stk.len() - 2];
+                let g = gcd(x, y);
+                if g == 1 {
+                    break;
+                }
+                stk.pop();
+                let last = stk.last_mut().unwrap();
+                *last = x / g * y;
+            }
+        }
+
+        stk.into_iter().map(|v| v as i32).collect()
+    }
+}