diff --git a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README.md b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README.md index 5f721e49abaf5..efd2bd7155e31 100644 --- a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README.md +++ b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README.md @@ -73,9 +73,9 @@ nums 中最大的数是 3 ### 方法一:模拟 -根据题意模拟即可,即先找出数组 `nums` 中的最大值和最小值,然后求最大值和最小值的最大公约数。 +我们根据题意模拟即可,即先找出数组 $\textit{nums}$ 中的最大值和最小值,然后求最大值和最小值的最大公约数。 -时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `nums` 的长度。 +时间复杂度 $O(n)$,其中 $n$ 为数组 $\textit{nums}$ 的长度。空间复杂度 $O(1)$。 @@ -112,9 +112,8 @@ class Solution { class Solution { public: int findGCD(vector& 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); } }; ``` @@ -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 { @@ -156,6 +151,27 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_gcd(nums: Vec) -> 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 +} +``` + diff --git a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README_EN.md b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README_EN.md index 0e5dfa62773f6..9270d2a8c181f 100644 --- a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README_EN.md +++ b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/README_EN.md @@ -72,7 +72,11 @@ The greatest common divisor of 3 and 3 is 3. -### 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)$. @@ -109,9 +113,8 @@ class Solution { class Solution { public: int findGCD(vector& 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); } }; ``` @@ -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 { @@ -153,6 +152,27 @@ function gcd(a: number, b: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_gcd(nums: Vec) -> 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 +} +``` + diff --git a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp index 8e091efa99238..8282534ace379 100644 --- a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp +++ b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: int findGCD(vector& 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); } -}; \ No newline at end of file +}; diff --git a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.rs b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.rs new file mode 100644 index 0000000000000..d47f5614953c4 --- /dev/null +++ b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.rs @@ -0,0 +1,16 @@ +impl Solution { + pub fn find_gcd(nums: Vec) -> 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 +} diff --git a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.ts b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.ts index dc48af0cbf935..5e4f09e93f680 100644 --- a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.ts +++ b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.ts @@ -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 {