当前位置:网站首页>Leetcode 1979. 找出数组的最大公约数
Leetcode 1979. 找出数组的最大公约数
2022-06-09 16:29:00 【我不是萧海哇~~~~】

给你一个整数数组 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 <= 1000
- 1 <= nums[i] <= 1000
Code:
class Solution {
public:
int findGCD(vector<int>& nums) {
sort(nums.begin(),nums.end());
int res=1;
for(int i=1;i<=nums[0];i++)
{
if(nums[0]%i==0 && (nums[nums.size()-1]%i==0))
{
res=i;
}
}
return res;
}
};
边栏推荐
- flutter系列之:Material主题的基础-MaterialApp
- ELK 不香了!我用 Graylog,轻量多了....
- 虚幻引擎插件 - Maya LiveLink - 安装和使用
- 【经验分享】Django开发中常用到的数据库操作总结
- How to use cloud notes to record and sort out the graduation notes given by teachers to students
- Using kubekey to build kubernetes/kubesphere environment
- How can we sustain a valuation of 60billion yuan for tea lovers who like the new and hate the old?
- [ctfshow singles cup]web writeup & learn the basic usage of SED awk
- Linux runs shengxunwei online customer service system: implementation method of supporting SQL server and MySQL at the same time
- DZ插件-免费DZ插件大全汇集所有插件功能
猜你喜欢

Querylist asynchronously fetches web page data

在初始化时启动mysql服务时显示net不是内部和外部的命令,也不是可运行的程序。如何解决该问题?这与net.exe缺失有关吗

阿里云618有奖体验:搭建基于OSS的图片分享网站

Attack and defense world (WEB) --web_ php_ include

Test must see, key points of writing test cases for the first time

使用ffmpeg合并mp4文件

flutter系列之:Material主题的基础-MaterialApp

记一次找因redis使用不当导致应用卡死bug的过程

Still building projects from scratch? This upgraded rapid development scaffold is worth a try~

浏览器指纹解读
随机推荐
不看后悔,appium自动化环境完美搭建
UEditor圖片跨域上傳解决方案
How to lock the screen with one button when leaving the computer midway
DZ插件-免费DZ插件大全汇集所有插件功能
ASEMI的MOS管9N90参数,9N90电路图,9N90实物图
恕我直言:程序员大部分时间不是在写代码,而是。。。
Google搜索为什么不能无限分页?
使用 KubeKey 搭建 Kubernetes/KubeSphere 环境的'心路(累)历程'
【无标题】
Windows下安装Redis及php的redis扩展
Redis implements paging
opensuse添加开机启动项
如何查找和删除Endnote中重复的文献
网页加载waiting(TTFB)时间过长的解决方案参考
PHP common date related functions
leetcode:240. Search 2D matrix II
鸿蒙 HiLog日志打印使用技巧
海通证券是安全的吗
关闭StackExchange等平台的privacy收集窗口
Browser fingerprint interpretation