Skip to content

feat: add solutions to lc problem: No.1979 #3872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2024
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 @@ -73,9 +73,9 @@ nums 中最大的数是 3

### 方法一:模拟

根据题意模拟即可,即先找出数组 `nums` 中的最大值和最小值,然后求最大值和最小值的最大公约数。
我们根据题意模拟即可,即先找出数组 $\textit{nums}$ 中的最大值和最小值,然后求最大值和最小值的最大公约数。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。
时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$

<!-- tabs:start -->

Expand Down Expand Up @@ -112,9 +112,8 @@ class Solution {
class Solution {
public:
int findGCD(vector<int>& nums) {
int a = *max_element(nums.begin(), nums.end());
int b = *min_element(nums.begin(), nums.end());
return gcd(a, b);
auto [min, max] = ranges::minmax_element(nums);
return gcd(*min, *max);
}
};
```
Expand All @@ -139,13 +138,9 @@ func gcd(a, b int) int {

```ts
function findGCD(nums: number[]): number {
let a = 1;
let b = 1000;
for (const x of nums) {
a = Math.max(a, x);
b = Math.min(b, x);
}
return gcd(a, b);
const min = Math.min(...nums);
const max = Math.max(...nums);
return gcd(min, max);
}

function gcd(a: number, b: number): number {
Expand All @@ -156,6 +151,27 @@ function gcd(a: number, b: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn find_gcd(nums: Vec<i32>) -> i32 {
let min_val = *nums.iter().min().unwrap();
let max_val = *nums.iter().max().unwrap();
gcd(min_val, max_val)
}
}

fn gcd(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ The greatest common divisor of 3 and 3 is 3.

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

We can simulate according to the problem description. First, find the maximum and minimum values in the array $\textit{nums}$, then find the greatest common divisor of the maximum and minimum values.

The time complexity is $O(n)$, where $n$ is the length of the array $\textit{nums}$. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -109,9 +113,8 @@ class Solution {
class Solution {
public:
int findGCD(vector<int>& nums) {
int a = *max_element(nums.begin(), nums.end());
int b = *min_element(nums.begin(), nums.end());
return gcd(a, b);
auto [min, max] = ranges::minmax_element(nums);
return gcd(*min, *max);
}
};
```
Expand All @@ -136,13 +139,9 @@ func gcd(a, b int) int {

```ts
function findGCD(nums: number[]): number {
let a = 1;
let b = 1000;
for (const x of nums) {
a = Math.max(a, x);
b = Math.min(b, x);
}
return gcd(a, b);
const min = Math.min(...nums);
const max = Math.max(...nums);
return gcd(min, max);
}

function gcd(a: number, b: number): number {
Expand All @@ -153,6 +152,27 @@ function gcd(a: number, b: number): number {
}
```

#### Rust

```rust
impl Solution {
pub fn find_gcd(nums: Vec<i32>) -> i32 {
let min_val = *nums.iter().min().unwrap();
let max_val = *nums.iter().max().unwrap();
gcd(min_val, max_val)
}
}

fn gcd(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
class Solution {
public:
int findGCD(vector<int>& nums) {
int a = *max_element(nums.begin(), nums.end());
int b = *min_element(nums.begin(), nums.end());
return gcd(a, b);
auto [min, max] = ranges::minmax_element(nums);
return gcd(*min, *max);
}
};
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
impl Solution {
pub fn find_gcd(nums: Vec<i32>) -> i32 {
let min_val = *nums.iter().min().unwrap();
let max_val = *nums.iter().max().unwrap();
gcd(min_val, max_val)
}
}

fn gcd(mut a: i32, mut b: i32) -> i32 {
while b != 0 {
let temp = b;
b = a % b;
a = temp;
}
a
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
function findGCD(nums: number[]): number {
let a = 1;
let b = 1000;
for (const x of nums) {
a = Math.max(a, x);
b = Math.min(b, x);
}
return gcd(a, b);
const min = Math.min(...nums);
const max = Math.max(...nums);
return gcd(min, max);
}

function gcd(a: number, b: number): number {
Expand Down
Loading