[LeetCode]#1979. Find Greatest Common Divisor of Array
Nov 19, 2021
Environment: Python 3.8
Key technique: math
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.
Analysis:
- Find maximum and minimum in nums.
- Calculate common divisor between 1 result.
Solution:
class Solution:
def findGCD(self, nums):
return math.gcd(max(nums), min(nums))
Submissions: