程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

LeetCode-1979. Find the greatest common divisor of the array_ Python

編輯:Python
  • Give you an array of integers nums , Returns the maximum common divisor of the maximum number and the minimum number in an array .

  • The greatest common divisor of two numbers is the largest positive integer that can be divided by two numbers .

Example 1:

Input :nums = [2,5,6,9,10]
Output :2
explain :
nums The smallest number is 2
nums The biggest number is 10
2 and 10 The greatest common divisor of 2

Example 2:

Input :nums = [7,5,6,8,3]
Output :1
explain :
nums The smallest number is 3
nums The biggest number is 8
3 and 8 The greatest common divisor of 1

Example 3:

Input :nums = [3,3]
Output :3
explain :
nums The smallest number is 3
nums The biggest number is 3
3 and 3 The greatest common divisor of 3

Tips :

2 <= nums.length <= 1000
1 <= nums[i] <= 1000

Program code

class Solution:
def findGCD(self, nums: List[int]) -> int:
min1 = min(nums)
max1 = max(nums)
ans = 0
for i in range(1, max1 + 1):
if min1 % i == 0 and max1 % i == 0:
ans = i
return ans

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved