Leetcode Note: Go - Find Greatest Common Divisor of Array

Find Greatest Common Divisor of Array - LeetCode
https://leetcode.com/problems/find-greatest-common-divisor-of-array/

  • Go 言語で取り組んだメモ

回答

Go simple solution - Find Greatest Common Divisor of Array - LeetCode
https://leetcode.com/problems/find-greatest-common-divisor-of-array/solutions/2462181/go-simple-solution/

func findGCD(nums []int) int {
    min, max := nums[0], nums[1]
    
    for _, num := range nums {
        if num > max {
            max = num
        }
        
        if num < min {
            min = num
        }
    }
    
    return GCD(min, max)
}

func GCD(a, b int) int {
	for b != 0 {
		t := b
		b = a % b
		a = t
	}
	return a
}