Skip to content

Commit d7374bc

Browse files
authored
feat: add solutions to lc problem: No.2197 (#4724)
1 parent 1927cec commit d7374bc

File tree

4 files changed

+213
-4
lines changed

4 files changed

+213
-4
lines changed

solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ tags:
6262
- (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,<em><strong>3</strong></em>,3] 。
6363
- (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,<em><strong>3</strong></em>] 。
6464
- (2, 2) 是一组非互质数,且 LCM(2, 2) = 2 。得到 nums = [<em><strong>2</strong></em>,1,1,3] 。
65-
现在,nums 中不存在相邻的非互质数。
66-
因此,修改后得到的最终数组是 [2,1,1,3] 。
65+
现在,nums 中不存在相邻的非互质数。
66+
因此,修改后得到的最终数组是 [2,1,1,3] 。
6767
注意,存在其他方法可以获得相同的最终数组。
6868
</pre>
6969

@@ -229,6 +229,79 @@ function replaceNonCoprimes(nums: number[]): number[] {
229229
}
230230
```
231231

232+
#### Rust
233+
234+
```rust
235+
impl Solution {
236+
pub fn replace_non_coprimes(nums: Vec<i32>) -> Vec<i32> {
237+
fn gcd(mut a: i64, mut b: i64) -> i64 {
238+
while b != 0 {
239+
let t = a % b;
240+
a = b;
241+
b = t;
242+
}
243+
a
244+
}
245+
246+
let mut stk: Vec<i64> = Vec::new();
247+
for x in nums {
248+
stk.push(x as i64);
249+
while stk.len() > 1 {
250+
let x = *stk.last().unwrap();
251+
let y = stk[stk.len() - 2];
252+
let g = gcd(x, y);
253+
if g == 1 {
254+
break;
255+
}
256+
stk.pop();
257+
let last = stk.last_mut().unwrap();
258+
*last = x / g * y;
259+
}
260+
}
261+
262+
stk.into_iter().map(|v| v as i32).collect()
263+
}
264+
}
265+
```
266+
267+
#### C#
268+
269+
```cs
270+
public class Solution {
271+
public IList<int> ReplaceNonCoprimes(int[] nums) {
272+
long Gcd(long a, long b) {
273+
while (b != 0) {
274+
long t = a % b;
275+
a = b;
276+
b = t;
277+
}
278+
return a;
279+
}
280+
281+
var stk = new List<long>();
282+
foreach (int num in nums) {
283+
stk.Add(num);
284+
while (stk.Count > 1) {
285+
long x = stk[stk.Count - 1];
286+
long y = stk[stk.Count - 2];
287+
long g = Gcd(x, y);
288+
if (g == 1) {
289+
break;
290+
}
291+
stk.RemoveAt(stk.Count - 1);
292+
stk[stk.Count - 1] = x / g * y;
293+
}
294+
}
295+
296+
var ans = new List<int>();
297+
foreach (long v in stk) {
298+
ans.Add((int)v);
299+
}
300+
return ans;
301+
}
302+
}
303+
```
304+
232305
<!-- tabs:end -->
233306

234307
<!-- solution:end -->

solution/2100-2199/2197.Replace Non-Coprime Numbers in Array/README_EN.md

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ tags:
4242
<pre>
4343
<strong>Input:</strong> nums = [6,4,3,2,7,6,2]
4444
<strong>Output:</strong> [12,7,6]
45-
<strong>Explanation:</strong>
45+
<strong>Explanation:</strong>
4646
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong><u>12</u></strong>,3,2,7,6,2].
4747
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong><u>12</u></strong>,2,7,6,2].
4848
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong><u>12</u></strong>,7,6,2].
@@ -57,7 +57,7 @@ Note that there are other ways to obtain the same resultant array.
5757
<pre>
5858
<strong>Input:</strong> nums = [2,2,1,1,3,3,3]
5959
<strong>Output:</strong> [2,1,1,3]
60-
<strong>Explanation:</strong>
60+
<strong>Explanation:</strong>
6161
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>,3].
6262
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>].
6363
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<u><strong>2</strong></u>,1,1,3].
@@ -227,6 +227,79 @@ function replaceNonCoprimes(nums: number[]): number[] {
227227
}
228228
```
229229

230+
#### Rust
231+
232+
```rust
233+
impl Solution {
234+
pub fn replace_non_coprimes(nums: Vec<i32>) -> Vec<i32> {
235+
fn gcd(mut a: i64, mut b: i64) -> i64 {
236+
while b != 0 {
237+
let t = a % b;
238+
a = b;
239+
b = t;
240+
}
241+
a
242+
}
243+
244+
let mut stk: Vec<i64> = Vec::new();
245+
for x in nums {
246+
stk.push(x as i64);
247+
while stk.len() > 1 {
248+
let x = *stk.last().unwrap();
249+
let y = stk[stk.len() - 2];
250+
let g = gcd(x, y);
251+
if g == 1 {
252+
break;
253+
}
254+
stk.pop();
255+
let last = stk.last_mut().unwrap();
256+
*last = x / g * y;
257+
}
258+
}
259+
260+
stk.into_iter().map(|v| v as i32).collect()
261+
}
262+
}
263+
```
264+
265+
#### C#
266+
267+
```cs
268+
public class Solution {
269+
public IList<int> ReplaceNonCoprimes(int[] nums) {
270+
long Gcd(long a, long b) {
271+
while (b != 0) {
272+
long t = a % b;
273+
a = b;
274+
b = t;
275+
}
276+
return a;
277+
}
278+
279+
var stk = new List<long>();
280+
foreach (int num in nums) {
281+
stk.Add(num);
282+
while (stk.Count > 1) {
283+
long x = stk[stk.Count - 1];
284+
long y = stk[stk.Count - 2];
285+
long g = Gcd(x, y);
286+
if (g == 1) {
287+
break;
288+
}
289+
stk.RemoveAt(stk.Count - 1);
290+
stk[stk.Count - 1] = x / g * y;
291+
}
292+
}
293+
294+
var ans = new List<int>();
295+
foreach (long v in stk) {
296+
ans.Add((int)v);
297+
}
298+
return ans;
299+
}
300+
}
301+
```
302+
230303
<!-- tabs:end -->
231304

232305
<!-- solution:end -->
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class Solution {
2+
public IList<int> ReplaceNonCoprimes(int[] nums) {
3+
long Gcd(long a, long b) {
4+
while (b != 0) {
5+
long t = a % b;
6+
a = b;
7+
b = t;
8+
}
9+
return a;
10+
}
11+
12+
var stk = new List<long>();
13+
foreach (int num in nums) {
14+
stk.Add(num);
15+
while (stk.Count > 1) {
16+
long x = stk[stk.Count - 1];
17+
long y = stk[stk.Count - 2];
18+
long g = Gcd(x, y);
19+
if (g == 1) {
20+
break;
21+
}
22+
stk.RemoveAt(stk.Count - 1);
23+
stk[stk.Count - 1] = x / g * y;
24+
}
25+
}
26+
27+
var ans = new List<int>();
28+
foreach (long v in stk) {
29+
ans.Add((int)v);
30+
}
31+
return ans;
32+
}
33+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
impl Solution {
2+
pub fn replace_non_coprimes(nums: Vec<i32>) -> Vec<i32> {
3+
fn gcd(mut a: i64, mut b: i64) -> i64 {
4+
while b != 0 {
5+
let t = a % b;
6+
a = b;
7+
b = t;
8+
}
9+
a
10+
}
11+
12+
let mut stk: Vec<i64> = Vec::new();
13+
for x in nums {
14+
stk.push(x as i64);
15+
while stk.len() > 1 {
16+
let x = *stk.last().unwrap();
17+
let y = stk[stk.len() - 2];
18+
let g = gcd(x, y);
19+
if g == 1 {
20+
break;
21+
}
22+
stk.pop();
23+
let last = stk.last_mut().unwrap();
24+
*last = x / g * y;
25+
}
26+
}
27+
28+
stk.into_iter().map(|v| v as i32).collect()
29+
}
30+
}

0 commit comments

Comments
 (0)