Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ tags:
- (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,<em><strong>3</strong></em>,3] 。
- (3, 3) 是一组非互质数,且 LCM(3, 3) = 3 。得到 nums = [2,2,1,1,<em><strong>3</strong></em>] 。
- (2, 2) 是一组非互质数,且 LCM(2, 2) = 2 。得到 nums = [<em><strong>2</strong></em>,1,1,3] 。
现在,nums 中不存在相邻的非互质数。
因此,修改后得到的最终数组是 [2,1,1,3] 。
现在,nums 中不存在相邻的非互质数。
因此,修改后得到的最终数组是 [2,1,1,3] 。
注意,存在其他方法可以获得相同的最终数组。
</pre>

Expand Down Expand Up @@ -229,6 +229,79 @@ function replaceNonCoprimes(nums: number[]): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn replace_non_coprimes(nums: Vec<i32>) -> Vec<i32> {
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<i64> = 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<int> 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<long>();
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<int>();
foreach (long v in stk) {
ans.Add((int)v);
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ tags:
<pre>
<strong>Input:</strong> nums = [6,4,3,2,7,6,2]
<strong>Output:</strong> [12,7,6]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
- (6, 4) are non-coprime with LCM(6, 4) = 12. Now, nums = [<strong><u>12</u></strong>,3,2,7,6,2].
- (12, 3) are non-coprime with LCM(12, 3) = 12. Now, nums = [<strong><u>12</u></strong>,2,7,6,2].
- (12, 2) are non-coprime with LCM(12, 2) = 12. Now, nums = [<strong><u>12</u></strong>,7,6,2].
Expand All @@ -57,7 +57,7 @@ Note that there are other ways to obtain the same resultant array.
<pre>
<strong>Input:</strong> nums = [2,2,1,1,3,3,3]
<strong>Output:</strong> [2,1,1,3]
<strong>Explanation:</strong>
<strong>Explanation:</strong>
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>,3].
- (3, 3) are non-coprime with LCM(3, 3) = 3. Now, nums = [2,2,1,1,<u><strong>3</strong></u>].
- (2, 2) are non-coprime with LCM(2, 2) = 2. Now, nums = [<u><strong>2</strong></u>,1,1,3].
Expand Down Expand Up @@ -227,6 +227,79 @@ function replaceNonCoprimes(nums: number[]): number[] {
}
```

#### Rust

```rust
impl Solution {
pub fn replace_non_coprimes(nums: Vec<i32>) -> Vec<i32> {
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<i64> = 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<int> 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<long>();
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<int>();
foreach (long v in stk) {
ans.Add((int)v);
}
return ans;
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Solution {
public IList<int> 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<long>();
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<int>();
foreach (long v in stk) {
ans.Add((int)v);
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
impl Solution {
pub fn replace_non_coprimes(nums: Vec<i32>) -> Vec<i32> {
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<i64> = 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()
}
}