原文链接: https://leetcode-cn.com/problems/find-greatest-common-divisor-of-array
英文原文
Given an integer array nums, return the greatest common divisor of the smallest number and largest number in nums.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
Example 1:
Input: nums = [2,5,6,9,10] Output: 2 Explanation: The smallest number in nums is 2. The largest number in nums is 10. The greatest common divisor of 2 and 10 is 2.
Example 2:
Input: nums = [7,5,6,8,3] Output: 1 Explanation: The smallest number in nums is 3. The largest number in nums is 8. The greatest common divisor of 3 and 8 is 1.
Example 3:
Input: nums = [3,3] Output: 3 Explanation: The smallest number in nums is 3. The largest number in nums is 3. The greatest common divisor of 3 and 3 is 3.
Constraints:
2 <= nums.length <= 10001 <= nums[i] <= 1000
中文题目
给你一个整数数组 nums ,返回数组中最大数和最小数的 最大公约数 。
两个数的 最大公约数 是能够被两个数整除的最大正整数。
示例 1:
输入:nums = [2,5,6,9,10] 输出:2 解释: nums 中最小的数是 2 nums 中最大的数是 10 2 和 10 的最大公约数是 2
示例 2:
输入:nums = [7,5,6,8,3] 输出:1 解释: nums 中最小的数是 3 nums 中最大的数是 8 3 和 8 的最大公约数是 1
示例 3:
输入:nums = [3,3] 输出:3 解释: nums 中最小的数是 3 nums 中最大的数是 3 3 和 3 的最大公约数是 3
提示:
2 <= nums.length <= 10001 <= nums[i] <= 1000
通过代码
高赞题解
5850. 找出数组的最大公约数
第一道题。
题目要求数组里最大的数字和最小的数字的最大公倍数
直接用c++自带的泛型算法,30秒解决它!
STL
class Solution {
public:
int findGCD(vector<int>& nums) {
return gcd(*max_element(nums.begin(), nums.end()), *min_element(nums.begin(), nums.end()));
}
};
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 8840 | 11074 | 79.8% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|