Skip to content

Commit

Permalink
leetcode: Add 1979
Browse files Browse the repository at this point in the history
  • Loading branch information
XuShaohua committed Sep 24, 2024
1 parent bc7f0b3 commit a3ce1f8
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 0 deletions.
19 changes: 19 additions & 0 deletions math/src/gcd.cpp
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;
}
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]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

build: main.cpp
g++ -g main.cpp -o main
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 src/leetcode/1979.find-greatest-common-divisor-of-array/main.cpp
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;
}
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);
}
}

0 comments on commit a3ce1f8

Please sign in to comment.