-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#include <cassert> | ||
|
||
int gcd(int a, int b) { | ||
assert(a >= b); | ||
if (b == 0) { | ||
return a; | ||
} else { | ||
return gcd(b, a % b); | ||
} | ||
} | ||
|
||
int lcm(int a, int b) { | ||
assert(a >= b); | ||
return a / gcd(a, b) * b; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/leetcode/1979.find-greatest-common-divisor-of-array/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "lc-1979-find-greatest-common-divisor-of-array" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] |
3 changes: 3 additions & 0 deletions
3
src/leetcode/1979.find-greatest-common-divisor-of-array/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
build: main.cpp | ||
g++ -g main.cpp -o main |
4 changes: 4 additions & 0 deletions
4
src/leetcode/1979.find-greatest-common-divisor-of-array/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
# 1979. 找出数组的最大公约数 Find Greatest Common Divisor of Array | ||
|
||
[问题描述](https://leetcode.com/problems/find-greatest-common-divisor-of-array) |
30 changes: 30 additions & 0 deletions
30
src/leetcode/1979.find-greatest-common-divisor-of-array/main.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
#include <cassert> | ||
|
||
#include <algorithm> | ||
#include <vector> | ||
|
||
int gcd(int a, int b) { | ||
assert(a >= b); | ||
if (b == 0) { | ||
return a; | ||
} else { | ||
return gcd(b, a % b); | ||
} | ||
} | ||
|
||
class Solution { | ||
public: | ||
int findGCD(std::vector<int>& nums) { | ||
const int min_num = *std::min_element(nums.cbegin(), nums.cend()); | ||
const int max_num = *std::max_element(nums.cbegin(), nums.cend()); | ||
return gcd(max_num, min_num); | ||
} | ||
}; | ||
|
||
int main() { | ||
return 0; | ||
} |
48 changes: 48 additions & 0 deletions
48
src/leetcode/1979.find-greatest-common-divisor-of-array/src/main.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2024 Xu Shaohua <[email protected]>. All rights reserved. | ||
// Use of this source is governed by General Public License that can be found | ||
// in the LICENSE file. | ||
|
||
fn gcd(a: i32, b: i32) -> i32 { | ||
debug_assert!(a > b && b >= 0); | ||
if b == 0 { | ||
a | ||
} else { | ||
gcd(b, a % b) | ||
} | ||
} | ||
|
||
pub fn find_gcd1(nums: Vec<i32>) -> i32 { | ||
debug_assert!(nums.len() >= 2); | ||
let mut min_val = nums[0]; | ||
let mut max_val = nums[0]; | ||
for num in nums { | ||
if num < min_val { | ||
min_val = num; | ||
} | ||
if num > max_val { | ||
max_val = num; | ||
} | ||
} | ||
gcd(max_val, min_val) | ||
} | ||
|
||
pub type SolutionFn = fn(Vec<i32>) -> i32; | ||
|
||
fn check_solution(func: SolutionFn) { | ||
let nums = vec![2, 5, 6, 9, 10]; | ||
assert_eq!(func(nums), 2); | ||
} | ||
|
||
fn main() { | ||
check_solution(find_gcd1); | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::{check_solution, find_gcd1}; | ||
|
||
#[test] | ||
fn test_find_gcd1() { | ||
check_solution(find_gcd1); | ||
} | ||
} |